home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gas_251.zip / bin_251 / bfd / elf32-hppa.c < prev    next >
C/C++ Source or Header  |  1994-09-27  |  97KB  |  2,943 lines

  1. /* BFD back-end for HP PA-RISC ELF files.
  2.    Copyright (C) 1990, 91, 92, 93, 94 Free Software Foundation, Inc.
  3.  
  4.    Written by
  5.  
  6.     Center for Software Science
  7.     Department of Computer Science
  8.     University of Utah
  9.  
  10. This file is part of BFD, the Binary File Descriptor library.
  11.  
  12. This program is free software; you can redistribute it and/or modify
  13. it under the terms of the GNU General Public License as published by
  14. the Free Software Foundation; either version 2 of the License, or
  15. (at your option) any later version.
  16.  
  17. This program is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20. GNU General Public License for more details.
  21.  
  22. You should have received a copy of the GNU General Public License
  23. along with this program; if not, write to the Free Software
  24. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  25.  
  26. #include "bfd.h"
  27. #include "sysdep.h"
  28. #include "bfdlink.h"
  29. #include "libbfd.h"
  30. #include "obstack.h"
  31. #include "libelf.h"
  32.  
  33.  
  34. /* HPPA symbol table extension entry types */
  35. enum elf32_hppa_symextn_types
  36. {
  37.   PARISC_SXT_NULL,
  38.   PARISC_SXT_SYMNDX,
  39.   PARISC_SXT_ARG_RELOC,
  40. };
  41.  
  42. /* These macros compose and decompose the value of a symextn entry:
  43.  
  44.    entry_type = ELF32_PARISC_SX_TYPE(word);
  45.    entry_value = ELF32_PARISC_SX_VAL(word);
  46.    word = ELF32_PARISC_SX_WORD(type,val);  */
  47.  
  48. #define ELF32_PARISC_SX_TYPE(p)        ((p) >> 24)
  49. #define ELF32_PARISC_SX_VAL(p)        ((p) & 0xFFFFFF)
  50. #define ELF32_PARISC_SX_WORD(type,val)    (((type) << 24) + (val & 0xFFFFFF))
  51.  
  52. /* The following was added facilitate implementation of the .hppa_symextn
  53.    section.  This section is built after the symbol table is built in the
  54.    elf_write_object_contents routine (called from bfd_close).  It is built
  55.    so late because it requires information that is not known until
  56.    the symbol and string table sections have been allocated, and
  57.    the symbol table has been built. */
  58.  
  59. #define SYMEXTN_SECTION_NAME ".PARISC.symext"
  60.  
  61. /* FIXME.  Are these external?  (For example used by GAS?).  If so the
  62.    names need to change to avoid namespace pollution, if not they should
  63.    be moved into elf32-hppa.c.  */
  64. typedef unsigned long symext_entryS;
  65. struct symext_chain
  66.   {
  67.     symext_entryS entry;
  68.     struct symext_chain *next;
  69.   };
  70.  
  71. typedef struct symext_chain symext_chainS;
  72.  
  73. /* We use three different hash tables to hold information for
  74.    linking PA ELF objects.
  75.  
  76.    The first is the elf32_hppa_link_hash_table which is derived
  77.    from the standard ELF linker hash table.  We use this as a place to
  78.    attach other hash tables and static information.
  79.  
  80.    The second is the stub hash table which is derived from the
  81.    base BFD hash table.  The stub hash table holds the information
  82.    necessary to build the linker stubs during a link.
  83.  
  84.    The last hash table keeps track of argument location information needed
  85.    to build hash tables.  Each function with nonzero argument location
  86.    bits will have an entry in this table.  */
  87.  
  88. /* Hash table for linker stubs.  */
  89.  
  90. struct elf32_hppa_stub_hash_entry
  91. {
  92.   /* Base hash table entry structure, we can get the name of the stub
  93.      (and thus know exactly what actions it performs) from the base
  94.      hash table entry.  */
  95.   struct bfd_hash_entry root;
  96.  
  97.   /* Offset of the beginning of this stub.  */
  98.   bfd_vma offset;
  99.  
  100.   /* Given the symbol's value and its section we can determine its final
  101.      value when building the stubs (so the stub knows where to jump.  */
  102.   symvalue target_value;
  103.   asection *target_section;
  104. };
  105.  
  106. struct elf32_hppa_stub_hash_table
  107. {
  108.   /* The hash table itself.  */
  109.   struct bfd_hash_table root;
  110.  
  111.   /* The stub BFD.  */
  112.   bfd *stub_bfd;
  113.  
  114.   /* Where to place the next stub.  */
  115.   bfd_byte *location;
  116.  
  117.   /* Current offset in the stub section.  */
  118.   unsigned int offset;
  119.  
  120. };
  121.  
  122. /* Hash table for argument location information.  */
  123.  
  124. struct elf32_hppa_args_hash_entry
  125. {
  126.   /* Base hash table entry structure.  */
  127.   struct bfd_hash_entry root;
  128.  
  129.   /* The argument location bits for this entry.  */
  130.   int arg_bits;
  131. };
  132.  
  133. struct elf32_hppa_args_hash_table
  134. {
  135.   /* The hash table itself.  */
  136.   struct bfd_hash_table root;
  137. };
  138.  
  139. struct elf32_hppa_link_hash_entry
  140. {
  141.   struct elf_link_hash_entry root;
  142. };
  143.  
  144. struct elf32_hppa_link_hash_table
  145. {
  146.   /* The main hash table.  */
  147.   struct elf_link_hash_table root;
  148.  
  149.   /* The stub hash table.  */
  150.   struct elf32_hppa_stub_hash_table *stub_hash_table;
  151.  
  152.   /* The argument relocation bits hash table.  */
  153.   struct elf32_hppa_args_hash_table *args_hash_table;
  154.  
  155.   /* A count of the number of output symbols.  */
  156.   unsigned int output_symbol_count;
  157.  
  158.   /* Stuff so we can handle DP relative relocations.  */
  159.   long global_value;
  160.   int global_sym_defined;
  161. };
  162.  
  163. typedef unsigned int symextn_entry;
  164.  
  165. /* FIXME.  */
  166. #define ARGUMENTS    0
  167. #define RETURN_VALUE    1
  168.  
  169. /* The various argument relocations that may be performed.  */
  170. typedef enum
  171. {
  172.   /* No relocation.  */
  173.   NO,
  174.   /* Relocate 32 bits from GR to FP register.  */
  175.   GF,
  176.   /* Relocate 64 bits from a GR pair to FP pair.  */
  177.   GD,
  178.   /* Relocate 32 bits from FP to GR.  */
  179.   FG,
  180.   /* Relocate 64 bits from FP pair to GR pair.  */
  181.   DG,
  182. } arg_reloc_type;
  183.  
  184. /* What is being relocated (eg which argument or the return value).  */
  185. typedef enum
  186. {
  187.   ARG0, ARG1, ARG2, ARG3, RET,
  188. } arg_reloc_location;
  189.  
  190.  
  191. /* ELF32/HPPA relocation support
  192.  
  193.     This file contains ELF32/HPPA relocation support as specified
  194.     in the Stratus FTX/Golf Object File Format (SED-1762) dated
  195.     February 1994.  */
  196.  
  197. #include "elf32-hppa.h"
  198. #include "hppa_stubs.h"
  199.  
  200. static bfd_reloc_status_type hppa_elf_reloc
  201.   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
  202.  
  203. static unsigned long hppa_elf_relocate_insn
  204.   PARAMS ((bfd *, asection *, unsigned long, unsigned long, long,
  205.        long, unsigned long, unsigned long, unsigned long));
  206.  
  207. static bfd_reloc_status_type hppa_elf_reloc
  208.   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd*, char **));
  209.  
  210. static CONST reloc_howto_type * elf_hppa_reloc_type_lookup
  211.   PARAMS ((bfd *, bfd_reloc_code_real_type));
  212.  
  213. static boolean elf32_hppa_set_section_contents
  214.   PARAMS ((bfd *, sec_ptr, PTR, file_ptr, bfd_size_type));
  215.  
  216. static void elf_info_to_howto
  217.   PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
  218.  
  219. static boolean elf32_hppa_backend_symbol_table_processing
  220.   PARAMS ((bfd *, elf_symbol_type *, int));
  221.  
  222. static void elf32_hppa_backend_begin_write_processing
  223.   PARAMS ((bfd *, struct bfd_link_info *));
  224.  
  225. static void elf32_hppa_backend_final_write_processing
  226.   PARAMS ((bfd *, boolean));
  227.  
  228. static void add_entry_to_symext_chain
  229.   PARAMS ((bfd *, unsigned int, unsigned int, symext_chainS **,
  230.        symext_chainS **));
  231.  
  232. static void
  233. elf_hppa_tc_make_sections PARAMS ((bfd *, symext_chainS *));
  234.  
  235. static boolean hppa_elf_is_local_label PARAMS ((bfd *, asymbol *));
  236.  
  237. static boolean elf32_hppa_add_symbol_hook
  238.   PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
  239.        const char **, flagword *, asection **, bfd_vma *));
  240.  
  241. static bfd_reloc_status_type elf32_hppa_bfd_final_link_relocate
  242.   PARAMS ((const reloc_howto_type *, bfd *, bfd *, asection *,
  243.        bfd_byte *, bfd_vma, bfd_vma, bfd_vma, struct bfd_link_info *,
  244.        asection *, const char *, int));
  245.  
  246. static struct bfd_link_hash_table *elf32_hppa_link_hash_table_create
  247.   PARAMS ((bfd *));
  248.  
  249. static struct bfd_hash_entry *
  250. elf32_hppa_stub_hash_newfunc
  251.   PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
  252.  
  253. static struct bfd_hash_entry *
  254. elf32_hppa_args_hash_newfunc
  255.   PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
  256.  
  257. static boolean
  258. elf32_hppa_relocate_section
  259.   PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *,
  260.        bfd_byte *, Elf_Internal_Rela *, Elf_Internal_Sym *, asection **,
  261.        char *));
  262.  
  263. static boolean
  264. elf32_hppa_stub_hash_table_init
  265.   PARAMS ((struct elf32_hppa_stub_hash_table *, bfd *,
  266.        struct bfd_hash_entry *(*) PARAMS ((struct bfd_hash_entry *,
  267.                            struct bfd_hash_table *,
  268.                            const char *))));
  269.  
  270. static boolean
  271. elf32_hppa_build_one_stub PARAMS ((struct bfd_hash_entry *, PTR));
  272.  
  273. static boolean
  274. elf32_hppa_read_symext_info
  275.   PARAMS ((bfd *, Elf_Internal_Shdr *, struct elf32_hppa_args_hash_table *,
  276.        Elf_Internal_Sym *, boolean, boolean));
  277.  
  278. static unsigned int elf32_hppa_size_of_stub
  279.   PARAMS ((unsigned int, unsigned int, bfd_vma, bfd_vma, const char *));
  280.  
  281. static boolean elf32_hppa_arg_reloc_needed
  282.   PARAMS ((unsigned int, unsigned int, arg_reloc_type []));
  283.  
  284. static void elf32_hppa_name_of_stub
  285.   PARAMS ((unsigned int, unsigned int, bfd_vma, bfd_vma, char *));
  286.  
  287. static boolean elf32_hppa_size_symext PARAMS ((struct bfd_hash_entry *, PTR));
  288.  
  289. static boolean elf32_hppa_link_output_symbol_hook
  290.   PARAMS ((bfd *, struct bfd_link_info *, const char *,
  291.        Elf_Internal_Sym *, asection *));
  292.  
  293. /* ELF/PA relocation howto entries.  */
  294.  
  295. static reloc_howto_type elf_hppa_howto_table[ELF_HOWTO_TABLE_SIZE] =
  296. {
  297.   {R_PARISC_NONE, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_NONE"},
  298.   {R_PARISC_DIR32, 0, 0, 32, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_DIR32"},
  299.   {R_PARISC_DIR21L, 0, 0, 21, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_DIR21L"},
  300.   {R_PARISC_DIR17R, 0, 0, 17, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_DIR17R"},
  301.   {R_PARISC_DIR17F, 0, 0, 17, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_DIR17F"},
  302.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  303.   {R_PARISC_DIR14R, 0, 0, 14, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_DIR14R"},
  304.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  305.  
  306.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  307.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  308.   {R_PARISC_PCREL21L, 0, 0, 21, true, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_PCREL21L"},
  309.   {R_PARISC_PCREL17R, 0, 0, 17, true, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_PCREL17R"},
  310.   {R_PARISC_PCREL17F, 0, 0, 17, true, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_PCREL17F"},
  311.   {R_PARISC_PCREL17C, 0, 0, 17, true, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_PCREL17C"},
  312.   {R_PARISC_PCREL14R, 0, 0, 14, true, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_PCREL14R"},
  313.   {R_PARISC_PCREL14F, 0, 0, 14, true, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_PCREL14F"},
  314.  
  315.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  316.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  317.   {R_PARISC_DPREL21L, 0, 0, 21, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_DPREL21L"},
  318.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  319.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  320.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  321.   {R_PARISC_DPREL14R, 0, 0, 14, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_DPREL14R"},
  322.   {R_PARISC_DPREL14F, 0, 0, 14, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_DPREL14F"},
  323.  
  324.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  325.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  326.   {R_PARISC_DLTREL21L, 0, 0, 21, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_DLTREL21L"},
  327.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  328.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  329.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  330.   {R_PARISC_DLTREL14R, 0, 0, 14, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_DLTREL14R"},
  331.   {R_PARISC_DLTREL14F, 0, 0, 14, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_DLTREL14F"},
  332.  
  333.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  334.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  335.   {R_PARISC_DLTIND21L, 0, 0, 21, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_DLTIND21L"},
  336.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  337.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  338.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  339.   {R_PARISC_DLTIND14R, 0, 0, 14, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_DLTIND14R"},
  340.   {R_PARISC_DLTIND14F, 0, 0, 14, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_DLTIND14F"},
  341.  
  342.   {R_PARISC_SETBASE, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_SETBASE"},
  343.   {R_PARISC_BASEREL32, 0, 0, 32, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_BASEREL32"},
  344.   {R_PARISC_BASEREL21L, 0, 0, 21, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_BASEREL21L"},
  345.   {R_PARISC_BASEREL17R, 0, 0, 17, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_BASEREL17R"},
  346.   {R_PARISC_BASEREL17F, 0, 0, 17, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_BASEREL17F"},
  347.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  348.   {R_PARISC_BASEREL14R, 0, 0, 14, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_BASEREL14R"},
  349.   {R_PARISC_BASEREL14F, 0, 0, 14, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_BASEREL14F"},
  350.  
  351.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  352.   {R_PARISC_TEXTREL32, 0, 0, 32, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_TEXTREL32"},
  353.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  354.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  355.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  356.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  357.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  358.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  359.  
  360.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  361.   {R_PARISC_DATAREL32, 0, 0, 32, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  362.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  363.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  364.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  365.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  366.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  367.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  368.  
  369.  
  370.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  371.   {R_PARISC_PLABEL32, 0, 0, 32, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_PLABEL32"},
  372.   {R_PARISC_PLABEL21L, 0, 0, 21, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_PLABEL21L"},
  373.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  374.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  375.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  376.   {R_PARISC_PLABEL14R, 0, 0, 14, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_PLABEL14R"},
  377.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  378.  
  379.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  380.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  381.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  382.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  383.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  384.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  385.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  386.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  387.  
  388.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  389.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  390.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  391.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  392.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  393.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  394.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  395.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  396.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  397.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  398.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  399.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  400.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  401.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  402.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  403.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  404.  
  405.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  406.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  407.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  408.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  409.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  410.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  411.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  412.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  413.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  414.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  415.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  416.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  417.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  418.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  419.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  420.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  421.  
  422.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  423.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  424.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  425.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  426.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  427.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  428.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  429.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  430.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  431.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  432.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  433.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  434.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  435.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  436.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  437.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  438.  
  439.  
  440.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  441.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  442.   {R_PARISC_PLTIND21L, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_PLTIND21L"},
  443.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  444.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  445.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_UNIMPLEMENTED"},
  446.   {R_PARISC_PLTIND14R, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_PLTIND14R"},
  447.   {R_PARISC_PLTIND14F, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_PLTIND14F"},
  448.  
  449.  
  450.   {R_PARISC_COPY, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_COPY"},
  451.   {R_PARISC_GLOB_DAT, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_GLOB_DAT"},
  452.   {R_PARISC_JMP_SLOT, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_JMP_SLOT"},
  453.   {R_PARISC_RELATIVE, 0, 0, 0, false, 0, complain_overflow_bitfield, hppa_elf_reloc, "R_PARISC_RELATIVE"},
  454.  
  455.   {R_PARISC_UNIMPLEMENTED, 0, 0, 0, false, 0, complain_overflow_dont, NULL, "R_PARISC_UNIMPLEMENTED"},
  456. };
  457.  
  458. /* Where (what register type) is an argument comming from?  */
  459. typedef enum
  460. {
  461.   AR_NO,
  462.   AR_GR,
  463.   AR_FR,
  464.   AR_FU,
  465.   AR_FPDBL1,
  466.   AR_FPDBL2,
  467. } arg_location;
  468.  
  469. /* Horizontal represents the callee's argument location information,
  470.    vertical represents caller's argument location information.  Value at a
  471.    particular X,Y location represents what (if any) argument relocation
  472.    needs to be performed to make caller and callee agree.  */
  473.  
  474. static CONST arg_reloc_type arg_mismatches[6][6] =
  475. {
  476.   {NO, NO, NO, NO, NO, NO},
  477.   {NO, NO, GF, NO, GD, NO},
  478.   {NO, FG, NO, NO, NO, NO},
  479.   {NO, NO, NO, NO, NO, NO},
  480.   {NO, DG, NO, NO, NO, NO},
  481.   {NO, DG, NO, NO, NO, NO},
  482. };
  483.  
  484. /* Likewise, but reversed for the return value.  */
  485. static CONST arg_reloc_type ret_mismatches[6][6] =
  486. {
  487.   {NO, NO, NO, NO, NO, NO},
  488.   {NO, NO, FG, NO, DG, NO},
  489.   {NO, GF, NO, NO, NO, NO},
  490.   {NO, NO, NO, NO, NO, NO},
  491.   {NO, GD, NO, NO, NO, NO},
  492.   {NO, GD, NO, NO, NO, NO},
  493. };
  494.  
  495. /* Misc static crud for symbol extension records.  */
  496. static symext_chainS *symext_rootP;
  497. static symext_chainS *symext_lastP;
  498. static int symext_chain_size;
  499.  
  500. /* FIXME: We should be able to try this static variable!  */
  501. static symext_entryS *symextn_contents;
  502.  
  503.  
  504. /* For linker stub hash tables.  */
  505. #define elf32_hppa_stub_hash_lookup(table, string, create, copy) \
  506.   ((struct elf32_hppa_stub_hash_entry *) \
  507.    bfd_hash_lookup (&(table)->root, (string), (create), (copy)))
  508.  
  509. #define elf32_hppa_stub_hash_traverse(table, func, info) \
  510.   (bfd_hash_traverse \
  511.    (&(table)->root, \
  512.     (boolean (*) PARAMS ((struct bfd_hash_entry *, PTR))) (func), \
  513.     (info)))
  514.  
  515. /* For linker args hash tables.  */
  516. #define elf32_hppa_args_hash_lookup(table, string, create, copy) \
  517.   ((struct elf32_hppa_args_hash_entry *) \
  518.    bfd_hash_lookup (&(table)->root, (string), (create), (copy)))
  519.  
  520. #define elf32_hppa_args_hash_traverse(table, func, info) \
  521.   (bfd_hash_traverse \
  522.    (&(table)->root, \
  523.     (boolean (*) PARAMS ((struct bfd_hash_entry *, PTR))) (func), \
  524.     (info)))
  525.  
  526. #define elf32_hppa_args_hash_table_init(table, newfunc) \
  527.   (bfd_hash_table_init \
  528.    (&(table)->root, \
  529.     (struct bfd_hash_entry *(*) PARAMS ((struct bfd_hash_entry *, \
  530.                      struct bfd_hash_table *, \
  531.                      const char *))) (newfunc)))
  532.  
  533. /* For HPPA linker hash table.  */
  534.  
  535. #define elf32_hppa_link_hash_lookup(table, string, create, copy, follow)\
  536.   ((struct elf32_hppa_link_hash_entry *)                \
  537.    elf_link_hash_lookup (&(table)->root, (string), (create),        \
  538.              (copy), (follow)))
  539.  
  540. #define elf32_hppa_link_hash_traverse(table, func, info)        \
  541.   (elf_link_hash_traverse                        \
  542.    (&(table)->root,                            \
  543.     (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func),    \
  544.     (info)))
  545.  
  546. /* Get the PA ELF linker hash table from a link_info structure.  */
  547.  
  548. #define elf32_hppa_hash_table(p) \
  549.   ((struct elf32_hppa_link_hash_table *) ((p)->hash))
  550.  
  551.  
  552. /* Extract specific argument location bits for WHICH from
  553.    the full argument location in AR.  */
  554. #define EXTRACT_ARBITS(ar, which) ((ar) >> (8 - ((which) * 2))) & 3
  555.  
  556. /* Assorted hash table functions.  */
  557.  
  558. /* Initialize an entry in the stub hash table.  */
  559.  
  560. static struct bfd_hash_entry *
  561. elf32_hppa_stub_hash_newfunc (entry, table, string)
  562.      struct bfd_hash_entry *entry;
  563.      struct bfd_hash_table *table;
  564.      const char *string;
  565. {
  566.   struct elf32_hppa_stub_hash_entry *ret;
  567.  
  568.   ret = (struct elf32_hppa_stub_hash_entry *) entry;
  569.  
  570.   /* Allocate the structure if it has not already been allocated by a
  571.      subclass.  */
  572.   if (ret == NULL)
  573.     ret = ((struct elf32_hppa_stub_hash_entry *)
  574.        bfd_hash_allocate (table,
  575.                   sizeof (struct elf32_hppa_stub_hash_entry)));
  576.   if (ret == NULL)
  577.     {
  578.       bfd_set_error (bfd_error_no_memory);
  579.       return NULL;
  580.     }
  581.  
  582.   /* Call the allocation method of the superclass.  */
  583.   ret = ((struct elf32_hppa_stub_hash_entry *)
  584.      bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
  585.  
  586.   if (ret)
  587.     {
  588.       /* Initialize the local fields.  */
  589.       ret->offset = 0;
  590.       ret->target_value = 0;
  591.       ret->target_section = NULL;
  592.     }
  593.  
  594.   return (struct bfd_hash_entry *) ret;
  595. }
  596.  
  597. /* Initialize a stub hash table.  */
  598.  
  599. static boolean
  600. elf32_hppa_stub_hash_table_init (table, stub_bfd, newfunc)
  601.      struct elf32_hppa_stub_hash_table *table;
  602.      bfd *stub_bfd;
  603.      struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
  604.                         struct bfd_hash_table *,
  605.                         const char *));
  606. {
  607.   table->offset = 0;
  608.   table->location = 0;
  609.   table->stub_bfd = stub_bfd;
  610.   return (bfd_hash_table_init (&table->root, newfunc));
  611. }
  612.  
  613. /* Initialize an entry in the argument location hash table.  */
  614.  
  615. static struct bfd_hash_entry *
  616. elf32_hppa_args_hash_newfunc (entry, table, string)
  617.      struct bfd_hash_entry *entry;
  618.      struct bfd_hash_table *table;
  619.      const char *string;
  620. {
  621.   struct elf32_hppa_args_hash_entry *ret;
  622.  
  623.   ret = (struct elf32_hppa_args_hash_entry *) entry;
  624.  
  625.   /* Allocate the structure if it has not already been allocated by a
  626.      subclass.  */
  627.   if (ret == NULL)
  628.     ret = ((struct elf32_hppa_args_hash_entry *)
  629.        bfd_hash_allocate (table,
  630.                   sizeof (struct elf32_hppa_args_hash_entry)));
  631.   if (ret == NULL)
  632.     {
  633.       bfd_set_error (bfd_error_no_memory);
  634.       return NULL;
  635.     }
  636.  
  637.   /* Call the allocation method of the superclass.  */
  638.   ret = ((struct elf32_hppa_args_hash_entry *)
  639.      bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
  640.  
  641.   /* Initialize the local fields.  */
  642.   if (ret)
  643.     ret->arg_bits = 0;
  644.  
  645.   return (struct bfd_hash_entry *) ret;
  646. }
  647.  
  648. /* Create the derived linker hash table.  The PA ELF port uses the derived
  649.    hash table to keep information specific to the PA ELF linker (without
  650.    using static variables).  */
  651.  
  652. static struct bfd_link_hash_table *
  653. elf32_hppa_link_hash_table_create (abfd)
  654.      bfd *abfd;
  655. {
  656.   struct elf32_hppa_link_hash_table *ret;
  657.  
  658.   ret = ((struct elf32_hppa_link_hash_table *)
  659.      bfd_alloc (abfd, sizeof (struct elf32_hppa_link_hash_table)));
  660.   if (ret == NULL)
  661.     {
  662.       bfd_set_error (bfd_error_no_memory);
  663.       return NULL;
  664.     }
  665.   if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
  666.                       _bfd_elf_link_hash_newfunc))
  667.     {
  668.       bfd_release (abfd, ret);
  669.       return NULL;
  670.     }
  671.   ret->stub_hash_table = NULL;
  672.   ret->args_hash_table = NULL;
  673.   ret->output_symbol_count = 0;
  674.   ret->global_value = 0;
  675.   ret->global_sym_defined = 0;
  676.  
  677.   return &ret->root.root;
  678. }
  679.  
  680. /* Relocate the given INSN given the various input parameters.
  681.  
  682.    FIXME: endianness and sizeof (long) issues abound here.  */
  683.  
  684. static unsigned long
  685. hppa_elf_relocate_insn (abfd, input_sect, insn, address, sym_value,
  686.             r_addend, r_format, r_field, pcrel)
  687.      bfd *abfd;
  688.      asection *input_sect;
  689.      unsigned long insn;
  690.      unsigned long address;
  691.      long sym_value;
  692.      long r_addend;
  693.      unsigned long r_format;
  694.      unsigned long r_field;
  695.      unsigned long pcrel;
  696. {
  697.   unsigned char opcode = get_opcode (insn);
  698.   long constant_value;
  699.  
  700.   switch (opcode)
  701.     {
  702.     case LDO:
  703.     case LDB:
  704.     case LDH:
  705.     case LDW:
  706.     case LDWM:
  707.     case STB:
  708.     case STH:
  709.     case STW:
  710.     case STWM:
  711.     case COMICLR:
  712.     case SUBI:
  713.     case ADDIT:
  714.     case ADDI:
  715.     case LDIL:
  716.     case ADDIL:
  717.       constant_value = HPPA_R_CONSTANT (r_addend);
  718.  
  719.       if (pcrel)
  720.     sym_value -= address;
  721.  
  722.       sym_value = hppa_field_adjust (sym_value, constant_value, r_field);
  723.       return hppa_rebuild_insn (abfd, insn, sym_value, r_format);
  724.  
  725.     case BL:
  726.     case BE:
  727.     case BLE:
  728.       /* XXX computing constant_value is not needed??? */
  729.       constant_value = assemble_17 ((insn & 0x001f0000) >> 16,
  730.                     (insn & 0x00001ffc) >> 2,
  731.                     insn & 1);
  732.  
  733.       constant_value = (constant_value << 15) >> 15;
  734.       if (pcrel)
  735.     {
  736.       sym_value -=
  737.         address + input_sect->output_offset
  738.         + input_sect->output_section->vma;
  739.       sym_value = hppa_field_adjust (sym_value, -8, r_field);
  740.     }
  741.       else
  742.     sym_value = hppa_field_adjust (sym_value, constant_value, r_field);
  743.  
  744.       return hppa_rebuild_insn (abfd, insn, sym_value >> 2, r_format);
  745.  
  746.     default:
  747.       if (opcode == 0)
  748.     {
  749.       constant_value = HPPA_R_CONSTANT (r_addend);
  750.  
  751.       if (pcrel)
  752.         sym_value -= address;
  753.  
  754.       return hppa_field_adjust (sym_value, constant_value, r_field);
  755.     }
  756.       else
  757.     abort ();
  758.     }
  759. }
  760.  
  761. /* Relocate an HPPA ELF section.  */
  762.  
  763. static boolean
  764. elf32_hppa_relocate_section (output_bfd, info, input_bfd, input_section,
  765.                  contents, relocs, local_syms, local_sections,
  766.                  output_names)
  767.      bfd *output_bfd;
  768.      struct bfd_link_info *info;
  769.      bfd *input_bfd;
  770.      asection *input_section;
  771.      bfd_byte *contents;
  772.      Elf_Internal_Rela *relocs;
  773.      Elf_Internal_Sym *local_syms;
  774.      asection **local_sections;
  775.      char *output_names;
  776. {
  777.   Elf_Internal_Shdr *symtab_hdr;
  778.   Elf_Internal_Rela *rel;
  779.   Elf_Internal_Rela *relend;
  780.  
  781.   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  782.  
  783.   rel = relocs;
  784.   relend = relocs + input_section->reloc_count;
  785.   for (; rel < relend; rel++)
  786.     {
  787.       int r_type;
  788.       const reloc_howto_type *howto;
  789.       long r_symndx;
  790.       struct elf_link_hash_entry *h;
  791.       Elf_Internal_Sym *sym;
  792.       asection *sym_sec;
  793.       bfd_vma relocation;
  794.       bfd_reloc_status_type r;
  795.       const char *sym_name;
  796.  
  797.       r_type = ELF32_R_TYPE (rel->r_info);
  798.       if (r_type < 0 || r_type >= (int) R_PARISC_UNIMPLEMENTED)
  799.     {
  800.       bfd_set_error (bfd_error_bad_value);
  801.       return false;
  802.     }
  803.       howto = elf_hppa_howto_table + r_type;
  804.  
  805.       r_symndx = ELF32_R_SYM (rel->r_info);
  806.  
  807.       if (info->relocateable)
  808.     {
  809.       /* This is a relocateable link.  We don't have to change
  810.          anything, unless the reloc is against a section symbol,
  811.          in which case we have to adjust according to where the
  812.          section symbol winds up in the output section.  */
  813.       if (r_symndx < symtab_hdr->sh_info)
  814.         {
  815.           sym = local_syms + r_symndx;
  816.           if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
  817.         {
  818.           sym_sec = local_sections[r_symndx];
  819.           rel->r_addend += sym_sec->output_offset;
  820.         }
  821.         }
  822.  
  823.       continue;
  824.     }
  825.  
  826.       /* This is a final link.  */
  827.       h = NULL;
  828.       sym = NULL;
  829.       sym_sec = NULL;
  830.       if (r_symndx < symtab_hdr->sh_info)
  831.     {
  832.       sym = local_syms + r_symndx;
  833.       sym_sec = local_sections[r_symndx];
  834.       relocation = ((ELF_ST_TYPE (sym->st_info) == STT_SECTION
  835.                ? 0 : sym->st_value)
  836.              + sym_sec->output_offset
  837.              + sym_sec->output_section->vma);
  838.     }
  839.       else
  840.     {
  841.       long indx;
  842.  
  843.       indx = r_symndx - symtab_hdr->sh_info;
  844.       h = elf_sym_hashes (input_bfd)[indx];
  845.       if (h->root.type == bfd_link_hash_defined)
  846.         {
  847.           sym_sec = h->root.u.def.section;
  848.           relocation = (h->root.u.def.value
  849.                 + sym_sec->output_offset
  850.                 + sym_sec->output_section->vma);
  851.         }
  852.       else if (h->root.type == bfd_link_hash_weak)
  853.         relocation = 0;
  854.       else
  855.         {
  856.           if (!((*info->callbacks->undefined_symbol)
  857.             (info, h->root.root.string, input_bfd,
  858.              input_section, rel->r_offset)))
  859.         return false;
  860.           relocation = 0;
  861.         }
  862.     }
  863.  
  864.       if (h != NULL)
  865.     sym_name = h->root.root.string;
  866.       else
  867.     {
  868.       sym_name = output_names + sym->st_name;
  869.       if (sym_name == NULL)
  870.         return false;
  871.       if (*sym_name == '\0')
  872.         sym_name = bfd_section_name (input_bfd, sym_sec);
  873.     }
  874.  
  875.       /* If args_hash_table is NULL, then we have encountered some
  876.      kind of link error (ex. undefined symbols).  Do not try to
  877.      apply any relocations, continue the loop so we can notify
  878.      the user of several errors in a single attempted link.  */
  879.       if (elf32_hppa_hash_table (info)->args_hash_table == NULL)
  880.     continue;
  881.  
  882.       r = elf32_hppa_bfd_final_link_relocate (howto, input_bfd, output_bfd,
  883.                           input_section, contents,
  884.                           rel->r_offset, relocation,
  885.                           rel->r_addend, info, sym_sec,
  886.                           sym_name, h == NULL);
  887.  
  888.       if (r != bfd_reloc_ok)
  889.     {
  890.       switch (r)
  891.         {
  892.         case bfd_reloc_dangerous:
  893.           {
  894.         /* We use this return value to indicate that we performed
  895.            a "dangerous" relocation.  This doesn't mean we did
  896.            the wrong thing, it just means there may be some cleanup
  897.            that needs to be done here.
  898.  
  899.            In particular we had to swap the last call insn and its
  900.            delay slot.  If the delay slot insn needed a relocation,
  901.            then we'll need to adjust the next relocation entry's
  902.            offset to account for the fact that the insn moved.
  903.  
  904.            This hair wouldn't be necessary if we inserted stubs
  905.            between procedures and used a "bl" to get to the stub.  */
  906.         if (rel != relend)
  907.           {
  908.             Elf_Internal_Rela *next_rel = rel + 1;
  909.  
  910.             if (rel->r_offset + 4 == next_rel->r_offset)
  911.               next_rel->r_offset -= 4;
  912.           }
  913.         break;
  914.           }
  915.         default:
  916.         case bfd_reloc_outofrange:
  917.         case bfd_reloc_overflow:
  918.           {
  919.         if (!((*info->callbacks->reloc_overflow)
  920.               (info, sym_name, howto->name, (bfd_vma) 0,
  921.             input_bfd, input_section, rel->r_offset)))
  922.           return false;
  923.           }
  924.           break;
  925.         }
  926.     }
  927.     }
  928.  
  929.   return true;
  930. }
  931.  
  932. /* Return one (or more) BFD relocations which implement the base
  933.    relocation with modifications based on format and field.  */
  934.  
  935. elf32_hppa_reloc_type **
  936. hppa_elf_gen_reloc_type (abfd, base_type, format, field)
  937.      bfd *abfd;
  938.      elf32_hppa_reloc_type base_type;
  939.      int format;
  940.      int field;
  941. {
  942.   elf32_hppa_reloc_type *finaltype;
  943.   elf32_hppa_reloc_type **final_types;
  944.  
  945.   /* Allocate slots for the BFD relocation.  */
  946.   final_types = (elf32_hppa_reloc_type **)
  947.     bfd_alloc_by_size_t (abfd, sizeof (elf32_hppa_reloc_type *) * 2);
  948.   if (final_types == NULL)
  949.     return NULL;
  950.  
  951.   /* Allocate space for the relocation itself.  */
  952.   finaltype = (elf32_hppa_reloc_type *)
  953.     bfd_alloc_by_size_t (abfd, sizeof (elf32_hppa_reloc_type));
  954.   if (finaltype == NULL)
  955.     return NULL;
  956.  
  957.   /* Some reasonable defaults.  */
  958.   final_types[0] = finaltype;
  959.   final_types[1] = NULL;
  960.  
  961. #define final_type finaltype[0]
  962.  
  963.   final_type = base_type;
  964.  
  965.   /* Just a tangle of nested switch statements to deal with the braindamage
  966.      that a different field selector means a completely different relocation
  967.      for PA ELF.  */
  968.   switch (base_type)
  969.     {
  970.     case R_HPPA:
  971.     case R_HPPA_ABS_CALL:
  972.       switch (format)
  973.     {
  974.     case 14:
  975.       switch (field)
  976.         {
  977.         case e_rsel:
  978.         case e_rrsel:
  979.           final_type = R_PARISC_DIR14R;
  980.           break;
  981.         case e_rtsel:
  982.           final_type = R_PARISC_DLTREL14R;
  983.           break;
  984.         case e_tsel:
  985.           final_type = R_PARISC_DLTREL14F;
  986.           break;
  987.         case e_rpsel:
  988.           final_type = R_PARISC_PLABEL14R;
  989.           break;
  990.         default:
  991.           return NULL;
  992.         }
  993.       break;
  994.  
  995.     case 17:
  996.       switch (field)
  997.         {
  998.         case e_fsel:
  999.           final_type = R_PARISC_DIR17F;
  1000.           break;
  1001.         case e_rsel:
  1002.         case e_rrsel:
  1003.           final_type = R_PARISC_DIR17R;
  1004.           break;
  1005.         default:
  1006.           return NULL;
  1007.         }
  1008.       break;
  1009.  
  1010.     case 21:
  1011.       switch (field)
  1012.         {
  1013.         case e_lsel:
  1014.         case e_lrsel:
  1015.           final_type = R_PARISC_DIR21L;
  1016.           break;
  1017.         case e_ltsel:
  1018.           final_type = R_PARISC_DLTREL21L;
  1019.           break;
  1020.         case e_lpsel:
  1021.           final_type = R_PARISC_PLABEL21L;
  1022.           break;
  1023.         default:
  1024.           return NULL;
  1025.         }
  1026.       break;
  1027.  
  1028.     case 32:
  1029.       switch (field)
  1030.         {
  1031.         case e_fsel:
  1032.           final_type = R_PARISC_DIR32;
  1033.           break;
  1034.         case e_psel:
  1035.           final_type = R_PARISC_PLABEL32;
  1036.           break;
  1037.         default:
  1038.           return NULL;
  1039.         }
  1040.       break;
  1041.  
  1042.     default:
  1043.       return NULL;
  1044.     }
  1045.       break;
  1046.  
  1047.  
  1048.     case R_HPPA_GOTOFF:
  1049.       switch (format)
  1050.     {
  1051.     case 14:
  1052.       switch (field)
  1053.         {
  1054.         case e_rsel:
  1055.         case e_rrsel:
  1056.           final_type = R_PARISC_DPREL14R;
  1057.           break;
  1058.         case e_fsel:
  1059.           final_type = R_PARISC_DPREL14F;
  1060.           break;
  1061.         default:
  1062.           return NULL;
  1063.         }
  1064.       break;
  1065.  
  1066.     case 21:
  1067.       switch (field)
  1068.         {
  1069.         case e_lrsel:
  1070.         case e_lsel:
  1071.           final_type = R_PARISC_DPREL21L;
  1072.           break;
  1073.         default:
  1074.           return NULL;
  1075.         }
  1076.       break;
  1077.  
  1078.     default:
  1079.       return NULL;
  1080.     }
  1081.       break;
  1082.  
  1083.  
  1084.     case R_HPPA_PCREL_CALL:
  1085.       switch (format)
  1086.     {
  1087.     case 14:
  1088.       switch (field)
  1089.         {
  1090.         case e_rsel:
  1091.         case e_rrsel:
  1092.           final_type = R_PARISC_PCREL14R;
  1093.           break;
  1094.         case e_fsel:
  1095.           final_type = R_PARISC_PCREL14F;
  1096.           break;
  1097.         default:
  1098.           return NULL;
  1099.         }
  1100.       break;
  1101.  
  1102.     case 17:
  1103.       switch (field)
  1104.         {
  1105.         case e_rsel:
  1106.         case e_rrsel:
  1107.           final_type = R_PARISC_PCREL17R;
  1108.           break;
  1109.         case e_fsel:
  1110.           final_type = R_PARISC_PCREL17F;
  1111.           break;
  1112.         default:
  1113.           return NULL;
  1114.         }
  1115.       break;
  1116.  
  1117.     case 21:
  1118.       switch (field)
  1119.         {
  1120.         case e_lsel:
  1121.         case e_lrsel:
  1122.           final_type = R_PARISC_PCREL21L;
  1123.           break;
  1124.         default:
  1125.           return NULL;
  1126.         }
  1127.       break;
  1128.  
  1129.     default:
  1130.       return NULL;
  1131.     }
  1132.       break;
  1133.  
  1134.     default:
  1135.       return NULL;
  1136.     }
  1137.  
  1138.   return final_types;
  1139. }
  1140.  
  1141. #undef final_type
  1142.  
  1143. /* Set the contents of a particular section at a particular location.  */
  1144.  
  1145. static boolean
  1146. elf32_hppa_set_section_contents (abfd, section, location, offset, count)
  1147.      bfd *abfd;
  1148.      sec_ptr section;
  1149.      PTR location;
  1150.      file_ptr offset;
  1151.      bfd_size_type count;
  1152. {
  1153.   /* Ignore write requests for the symbol extension section until we've
  1154.      had the chance to rebuild it ourselves.  */
  1155.   if (!strcmp (section->name, ".PARISC.symextn") && !symext_chain_size)
  1156.     return true;
  1157.   else
  1158.     return bfd_elf32_set_section_contents (abfd, section, location,
  1159.                        offset, count);
  1160. }
  1161.  
  1162. /* Translate from an elf into field into a howto relocation pointer.  */
  1163.  
  1164. static void
  1165. elf_info_to_howto (abfd, cache_ptr, dst)
  1166.      bfd *abfd;
  1167.      arelent *cache_ptr;
  1168.      Elf32_Internal_Rela *dst;
  1169. {
  1170.   BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_PARISC_UNIMPLEMENTED);
  1171.   cache_ptr->howto = &elf_hppa_howto_table[ELF32_R_TYPE (dst->r_info)];
  1172. }
  1173.  
  1174.  
  1175. /* Actually perform a relocation.  NOTE this is (mostly) superceeded
  1176.    by elf32_hppa_bfd_final_link_relocate which is called by the new
  1177.    fast linker.  */
  1178.  
  1179. static bfd_reloc_status_type
  1180. hppa_elf_reloc (abfd, reloc_entry, symbol_in, data, input_section, output_bfd,
  1181.         error_message)
  1182.      bfd *abfd;
  1183.      arelent *reloc_entry;
  1184.      asymbol *symbol_in;
  1185.      PTR data;
  1186.      asection *input_section;
  1187.      bfd *output_bfd;
  1188.      char **error_message;
  1189. {
  1190.   /* It is no longer valid to call hppa_elf_reloc when creating
  1191.      a final executable.  */
  1192.   if (output_bfd)
  1193.     {
  1194.       reloc_entry->address += input_section->output_offset;
  1195.  
  1196.       /* Work around lossage in generic elf code to write relocations.
  1197.      (maps different section symbols into the same symbol index).  */
  1198.       if ((symbol_in->flags & BSF_SECTION_SYM)
  1199.       && symbol_in->section)
  1200.     reloc_entry->addend += symbol_in->section->output_offset;
  1201.       return bfd_reloc_ok;
  1202.     }
  1203.   else
  1204.     {
  1205.       *error_message = (char *) "Unsupported call to hppa_elf_reloc";
  1206.       return bfd_reloc_notsupported;
  1207.     }
  1208. }
  1209.  
  1210. /* Actually perform a relocation as part of a final link.  This can get
  1211.    rather hairy when linker stubs are needed.  */
  1212.  
  1213. static bfd_reloc_status_type
  1214. elf32_hppa_bfd_final_link_relocate (howto, input_bfd, output_bfd,
  1215.                     input_section, contents, offset, value,
  1216.                     addend, info, sym_sec, sym_name, is_local)
  1217.      const reloc_howto_type *howto;
  1218.      bfd *input_bfd;
  1219.      bfd *output_bfd;
  1220.      asection *input_section;
  1221.      bfd_byte *contents;
  1222.      bfd_vma offset;
  1223.      bfd_vma value;
  1224.      bfd_vma addend;
  1225.      struct bfd_link_info *info;
  1226.      asection *sym_sec;
  1227.      const char *sym_name;
  1228.      int is_local;
  1229. {
  1230.   unsigned long insn;
  1231.   unsigned long r_type = howto->type;
  1232.   unsigned long r_format = howto->bitsize;
  1233.   unsigned long r_field = e_fsel;
  1234.   bfd_byte *hit_data = contents + offset + input_section->vma;
  1235.   boolean r_pcrel = howto->pc_relative;
  1236.  
  1237.   insn = bfd_get_32 (input_bfd, hit_data);
  1238.  
  1239.   /* Make sure we have a value for $global$.  FIXME isn't this effectively
  1240.      just like the gp pointer on MIPS?  Can we use those routines for this
  1241.      purpose?  */
  1242.   if (!elf32_hppa_hash_table (info)->global_sym_defined)
  1243.     {
  1244.       struct elf_link_hash_entry *h;
  1245.       asection *sec;
  1246.  
  1247.       h = elf_link_hash_lookup (elf_hash_table (info), "$global$", false,
  1248.                  false, false);
  1249.  
  1250.       /* If there isn't a $global$, then we're in deep trouble.  */
  1251.       if (h == NULL)
  1252.     return bfd_reloc_notsupported;
  1253.  
  1254.       sec = h->root.u.def.section;
  1255.       elf32_hppa_hash_table (info)->global_value = (h->root.u.def.value
  1256.                             + sec->vma
  1257.                             + sec->output_offset);
  1258.       elf32_hppa_hash_table (info)->global_sym_defined = 1;
  1259.     }
  1260.  
  1261.   switch (r_type)
  1262.     {
  1263.     case R_PARISC_NONE:
  1264.       break;
  1265.  
  1266.     case R_PARISC_DIR32:
  1267.     case R_PARISC_DIR17F:
  1268.     case R_PARISC_PCREL17C:
  1269.       r_field = e_fsel;
  1270.       goto do_basic_type_1;
  1271.     case R_PARISC_DIR21L:
  1272.     case R_PARISC_PCREL21L:
  1273.       r_field = e_lrsel;
  1274.       goto do_basic_type_1;
  1275.     case R_PARISC_DIR17R:
  1276.     case R_PARISC_PCREL17R:
  1277.     case R_PARISC_DIR14R:
  1278.     case R_PARISC_PCREL14R:
  1279.       r_field = e_rrsel;
  1280.       goto do_basic_type_1;
  1281.  
  1282.     /* For all the DP relative relocations, we need to examine the symbol's
  1283.        section.  If it's a code section, then "data pointer relative" makes
  1284.        no sense.  In that case we don't adjust the "value", and for 21 bit
  1285.        addil instructions, we change the source addend register from %dp to
  1286.        %r0.  */
  1287.     case R_PARISC_DPREL21L:
  1288.       r_field = e_lrsel;
  1289.       if (sym_sec->flags & SEC_CODE)
  1290.     insn &= ~0x03e00000;
  1291.       else
  1292.     value -= elf32_hppa_hash_table (info)->global_value;
  1293.       goto do_basic_type_1;
  1294.     case R_PARISC_DPREL14R:
  1295.       r_field = e_rrsel;
  1296.       if ((sym_sec->flags & SEC_CODE) == 0)
  1297.     value -= elf32_hppa_hash_table (info)->global_value;
  1298.       goto do_basic_type_1;
  1299.     case R_PARISC_DPREL14F:
  1300.       r_field = e_fsel;
  1301.       if ((sym_sec->flags & SEC_CODE) == 0)
  1302.     value -= elf32_hppa_hash_table (info)->global_value;
  1303.       goto do_basic_type_1;
  1304.  
  1305.     /* These cases are separate as they may involve a lot more work
  1306.        to deal with linker stubs.  */
  1307.     case R_PARISC_PLABEL32:
  1308.     case R_PARISC_PLABEL21L:
  1309.     case R_PARISC_PLABEL14R:
  1310.     case R_PARISC_PCREL17F:
  1311.       {
  1312.     bfd_vma location;
  1313.     unsigned int len, caller_args, callee_args;
  1314.     arg_reloc_type arg_reloc_types[5];
  1315.     struct elf32_hppa_args_hash_table *args_hash_table;
  1316.     struct elf32_hppa_args_hash_entry *args_hash;
  1317.     char *new_name, *stub_name;
  1318.  
  1319.     /* Get the field selector right.  We'll need it in a minute.  */
  1320.     if (r_type == R_PARISC_PCREL17F
  1321.         || r_type == R_PARISC_PLABEL32)
  1322.       r_field = e_fsel;
  1323.     else if (r_type == R_PARISC_PLABEL21L)
  1324.       r_field = e_lrsel;
  1325.     else if (r_type == R_PARISC_PLABEL14R)
  1326.       r_field = e_rrsel;
  1327.  
  1328.     /* Find out where we are and where we're going.  */
  1329.     location = (offset +
  1330.             input_section->output_offset +
  1331.             input_section->output_section->vma);
  1332.  
  1333.     /* Now look for the argument relocation bits associated with the
  1334.        target.  */
  1335.     len = strlen (sym_name) + 1;
  1336.     if (is_local)
  1337.       len += 9;
  1338.     new_name = malloc (len);
  1339.     if (!new_name)
  1340.       {
  1341.         bfd_set_error (bfd_error_no_memory);
  1342.         return bfd_reloc_notsupported;
  1343.       }
  1344.     strcpy (new_name, sym_name);
  1345.  
  1346.     /* Local symbols have unique IDs.  */
  1347.     if (is_local)
  1348.       sprintf (new_name + len - 10, "_%08x", (int)sym_sec);
  1349.  
  1350.     args_hash_table = elf32_hppa_hash_table (info)->args_hash_table;
  1351.  
  1352.     args_hash = elf32_hppa_args_hash_lookup (args_hash_table,
  1353.                          new_name, false, false);
  1354.     if (args_hash == NULL)
  1355.       callee_args = 0;
  1356.     else
  1357.       callee_args = args_hash->arg_bits;
  1358.  
  1359.     /* If this is a CALL relocation, then get the caller's bits
  1360.        from the addend.  Else use the magic 0x155 value for PLABELS.
  1361.  
  1362.        Also we don't care about the destination (value) for PLABELS.  */
  1363.     if (r_type == R_PARISC_PCREL17F)
  1364.       caller_args = HPPA_R_ARG_RELOC (addend);
  1365.     else
  1366.       {
  1367.         caller_args = 0x155;
  1368.         location = value;
  1369.       }
  1370.  
  1371.     /* Any kind of linker stub needed?  */
  1372.     if (((int)(value - location) > 0x3ffff)
  1373.         || ((int)(value - location) < (int)0xfffc0000)
  1374.         || elf32_hppa_arg_reloc_needed (caller_args, callee_args,
  1375.                         arg_reloc_types))
  1376.       {
  1377.         struct elf32_hppa_stub_hash_table *stub_hash_table;
  1378.         struct elf32_hppa_stub_hash_entry *stub_hash;
  1379.         asection *stub_section;
  1380.  
  1381.         /* Build a name for the stub.  */
  1382.  
  1383.         len = strlen (new_name);
  1384.         len += 23;
  1385.         stub_name = malloc (len);
  1386.         if (!stub_name)
  1387.           {
  1388.         bfd_set_error (bfd_error_no_memory);
  1389.         return bfd_reloc_notsupported;
  1390.           }
  1391.         elf32_hppa_name_of_stub (caller_args, callee_args,
  1392.                      location, value, stub_name);
  1393.         strcat (stub_name, new_name);
  1394.         free (new_name);
  1395.  
  1396.         stub_hash_table = elf32_hppa_hash_table (info)->stub_hash_table;
  1397.  
  1398.         stub_hash
  1399.           = elf32_hppa_stub_hash_lookup (stub_hash_table, stub_name,
  1400.                          false, false);
  1401.  
  1402.         /* We're done with that name.  */
  1403.         free (stub_name);
  1404.  
  1405.         /* The stub BFD only has one section.  */
  1406.         stub_section = stub_hash_table->stub_bfd->sections;
  1407.  
  1408.         if (stub_hash != NULL)
  1409.           {
  1410.  
  1411.         if (r_type == R_PARISC_PCREL17F)
  1412.           {
  1413.             unsigned long delay_insn;
  1414.             unsigned int opcode, rtn_reg, ldo_target_reg, ldo_src_reg;
  1415.  
  1416.             /* We'll need to peek at the next insn.  */
  1417.             delay_insn = bfd_get_32 (input_bfd, hit_data + 4);
  1418.             opcode = get_opcode (delay_insn);
  1419.  
  1420.             /* We also need to know the return register for this
  1421.                call.  */
  1422.             rtn_reg = (insn & 0x03e00000) >> 21;
  1423.  
  1424.             ldo_src_reg = (delay_insn & 0x03e00000) >> 21;
  1425.             ldo_target_reg = (delay_insn & 0x001f0000) >> 16;
  1426.  
  1427.             /* Munge up the value and other parameters for
  1428.                hppa_elf_relocate_insn.  */
  1429.  
  1430.             value = (stub_hash->offset
  1431.                  + stub_section->output_offset
  1432.                  + stub_section->output_section->vma);
  1433.  
  1434.             r_format = 17;
  1435.             r_field = e_fsel;
  1436.             r_pcrel = 0;
  1437.             addend = 0;
  1438.  
  1439.             /* We need to peek at the delay insn and determine if
  1440.                we'll need to swap the branch and its delay insn.  */
  1441.             if ((insn & 2)
  1442.             || (opcode == LDO
  1443.                 && ldo_target_reg == rtn_reg)
  1444.             || (delay_insn == 0x08000240))
  1445.               {
  1446.             /* No need to swap the branch and its delay slot, but
  1447.                we do need to make sure to jump past the return
  1448.                pointer update in the stub.  */
  1449.             value += 4;
  1450.  
  1451.             /* If the delay insn does a return pointer adjustment,
  1452.                then we have to make sure it stays valid.  */
  1453.             if (opcode == LDO
  1454.                 && ldo_target_reg == rtn_reg)
  1455.               {
  1456.                 delay_insn &= 0xfc00ffff;
  1457.                 delay_insn |= ((31 << 21) | (31 << 16));
  1458.                 bfd_put_32 (input_bfd, delay_insn, hit_data + 4);
  1459.               }
  1460.             /* Use a BLE to reach the stub.  */
  1461.             insn = BLE_SR4_R0;
  1462.               }
  1463.             else
  1464.               {
  1465.             /* Wonderful, we have to swap the call insn and its
  1466.                delay slot.  */
  1467.             bfd_put_32 (input_bfd, delay_insn, hit_data);
  1468.             /* Use a BLE,n to reach the stub.  */
  1469.             insn = (BLE_SR4_R0 | 0x2);
  1470.             bfd_put_32 (input_bfd, insn, hit_data + 4);
  1471.             insn = hppa_elf_relocate_insn (input_bfd,
  1472.                                input_section,
  1473.                                insn, offset + 4,
  1474.                                value, addend,
  1475.                                r_format, r_field,
  1476.                                r_pcrel);
  1477.             /* Update the instruction word.  */
  1478.             bfd_put_32 (input_bfd, insn, hit_data + 4);
  1479.             return bfd_reloc_dangerous;
  1480.               }
  1481.           }
  1482.         else
  1483.           {
  1484.             /* PLABEL stuff is easy.  */
  1485.  
  1486.             value = (stub_hash->offset
  1487.                  + stub_section->output_offset
  1488.                  + stub_section->output_section->vma);
  1489.             /* We don't need the RP adjustment for PLABELs.  */
  1490.             value += 4;
  1491.             if (r_type == R_PARISC_PLABEL32)
  1492.               r_format = 32;
  1493.             else if (r_type == R_PARISC_PLABEL21L)
  1494.               r_format = 21;
  1495.             else if (r_type == R_PARISC_PLABEL14R)
  1496.               r_format = 14;
  1497.  
  1498.             r_pcrel = 0;
  1499.             addend = 0;
  1500.           }
  1501.         }
  1502.           else
  1503.         return bfd_reloc_notsupported;
  1504.       }
  1505.     goto do_basic_type_1;
  1506.       }
  1507.  
  1508. do_basic_type_1:
  1509.       insn = hppa_elf_relocate_insn (input_bfd, input_section, insn,
  1510.                      offset, value, addend, r_format,
  1511.                      r_field, r_pcrel);
  1512.       break;
  1513.  
  1514.     /* Something we don't know how to handle.  */
  1515.     default:
  1516.       return bfd_reloc_notsupported;
  1517.     }
  1518.  
  1519.   /* Update the instruction word.  */
  1520.   bfd_put_32 (input_bfd, insn, hit_data);
  1521.   return (bfd_reloc_ok);
  1522. }
  1523.  
  1524. /* Return the address of the howto table entry to perform the CODE
  1525.    relocation for an ARCH machine.  */
  1526.  
  1527. static CONST reloc_howto_type *
  1528. elf_hppa_reloc_type_lookup (abfd, code)
  1529.      bfd *abfd;
  1530.      bfd_reloc_code_real_type code;
  1531. {
  1532.   if ((int) code < (int) R_PARISC_UNIMPLEMENTED)
  1533.     {
  1534.       BFD_ASSERT ((int) elf_hppa_howto_table[(int) code].type == (int) code);
  1535.       return &elf_hppa_howto_table[(int) code];
  1536.     }
  1537.   return NULL;
  1538. }
  1539.  
  1540. /* Return true if SYM represents a local label symbol.  */
  1541.  
  1542. static boolean
  1543. hppa_elf_is_local_label (abfd, sym)
  1544.      bfd *abfd;
  1545.      asymbol *sym;
  1546. {
  1547.   return (sym->name[0] == 'L' && sym->name[1] == '$');
  1548. }
  1549.  
  1550. /* Do any backend specific processing when beginning to write an object
  1551.    file.  For PA ELF we need to determine the size of the symbol extension
  1552.    section *before* any other output processing happens.  */
  1553.  
  1554. static void
  1555. elf32_hppa_backend_begin_write_processing (abfd, info)
  1556.      bfd *abfd;
  1557.      struct bfd_link_info *info;
  1558. {
  1559.   unsigned int i;
  1560.   asection *symextn_sec;
  1561.  
  1562.   /* Size up the symbol extension section.  */
  1563.   if ((abfd->outsymbols == NULL
  1564.        && info == NULL)
  1565.       || symext_chain_size != 0)
  1566.     return;
  1567.  
  1568.   if (info == NULL)
  1569.     {
  1570.       /* We were not called from the BFD ELF linker code, so we need
  1571.      to examine the output BFD's outsymbols.
  1572.  
  1573.      Note we can not build the symbol extensions now as the symbol
  1574.      map hasn't been set up.  */
  1575.       for (i = 0; i < abfd->symcount; i++)
  1576.     {
  1577.       elf_symbol_type *symbol = (elf_symbol_type *)abfd->outsymbols[i];
  1578.  
  1579.       /* Only functions ever need an entry in the symbol extension
  1580.          section.  */
  1581.       if (!(symbol->symbol.flags & BSF_FUNCTION))
  1582.         continue;
  1583.  
  1584.       /* And only if they specify the locations of their arguments.  */
  1585.       if (symbol->tc_data.hppa_arg_reloc == 0)
  1586.         continue;
  1587.  
  1588.       /* Yup.  This function symbol needs an entry.  */
  1589.       symext_chain_size += 2 * sizeof (symext_entryS);
  1590.     }
  1591.     }
  1592.   else if (info->relocateable == true)
  1593.     {
  1594.       struct elf32_hppa_args_hash_table *table;
  1595.       table = elf32_hppa_hash_table (info)->args_hash_table;
  1596.  
  1597.       /* Determine the size of the symbol extension section.  */
  1598.       elf32_hppa_args_hash_traverse (table,
  1599.                      elf32_hppa_size_symext,
  1600.                      &symext_chain_size);
  1601.     }
  1602.  
  1603.   /* Now create the section and set its size.  We'll fill in the
  1604.      contents later.  */
  1605.   symextn_sec = bfd_get_section_by_name (abfd, SYMEXTN_SECTION_NAME);
  1606.   if (symextn_sec == NULL)
  1607.     symextn_sec = bfd_make_section (abfd, SYMEXTN_SECTION_NAME);
  1608.  
  1609.   bfd_set_section_flags (abfd, symextn_sec,
  1610.              SEC_LOAD | SEC_HAS_CONTENTS | SEC_DATA);
  1611.   symextn_sec->output_section = symextn_sec;
  1612.   symextn_sec->output_offset = 0;
  1613.   bfd_set_section_alignment (abfd, symextn_sec, 2);
  1614.   bfd_set_section_size (abfd, symextn_sec, symext_chain_size);
  1615. }
  1616.  
  1617. /* Called for each entry in the args location hash table.  For each
  1618.    entry we bump the size pointer by 2 records (16 bytes).  */
  1619.  
  1620. static boolean
  1621. elf32_hppa_size_symext (gen_entry, in_args)
  1622.      struct bfd_hash_entry *gen_entry;
  1623.      PTR in_args;
  1624. {
  1625.   unsigned int *sizep = (unsigned int *)in_args;
  1626.  
  1627.   *sizep += 2 * sizeof (symext_entryS);
  1628.   return true;
  1629. }
  1630.  
  1631. /* Backend routine called by the linker for each output symbol.
  1632.  
  1633.    For PA ELF we use this opportunity to add an appropriate entry
  1634.    to the symbol extension chain for function symbols.  */
  1635.  
  1636. static boolean
  1637. elf32_hppa_link_output_symbol_hook (abfd, info, name, sym, section)
  1638.      bfd *abfd;
  1639.      struct bfd_link_info *info;
  1640.      const char *name;
  1641.      Elf_Internal_Sym *sym;
  1642.      asection *section;
  1643. {
  1644.   char *new_name;
  1645.   unsigned int len, index;
  1646.   struct elf32_hppa_args_hash_table *args_hash_table;
  1647.   struct elf32_hppa_args_hash_entry *args_hash;
  1648.  
  1649.   /* If the args hash table is NULL, then we've encountered an error
  1650.      of some sorts (for example, an undefined symbol).  In that case
  1651.      we've got nothing else to do.
  1652.  
  1653.      NOTE: elf_link_output_symbol will abort if we return false here!  */
  1654.   if (elf32_hppa_hash_table (info)->args_hash_table == NULL)
  1655.     return true;
  1656.  
  1657.   index = elf32_hppa_hash_table (info)->output_symbol_count++;
  1658.  
  1659.   /* We need to look up this symbol in the args hash table to see if
  1660.      it has argument relocation bits.  */
  1661.   if (ELF_ST_TYPE (sym->st_info) != STT_FUNC)
  1662.     return true;
  1663.  
  1664.   /* We know it's a function symbol of some kind.  */
  1665.   len = strlen (name) + 1;
  1666.   if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
  1667.     len += 9;
  1668.  
  1669.   new_name = malloc (len);
  1670.   if (new_name == NULL)
  1671.     {
  1672.       bfd_set_error (bfd_error_no_memory);
  1673.       return false;
  1674.     }
  1675.  
  1676.   strcpy (new_name, name);
  1677.   if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
  1678.     sprintf (new_name + len - 10, "_%08x", (int)section);
  1679.  
  1680.   /* Now that we have the unique name, we can look it up in the
  1681.      args hash table.  */
  1682.   args_hash_table = elf32_hppa_hash_table (info)->args_hash_table;
  1683.   args_hash = elf32_hppa_args_hash_lookup (args_hash_table, new_name,
  1684.                        false, false);
  1685.   free (new_name);
  1686.   if (args_hash == NULL)
  1687.     return true;
  1688.  
  1689.   /* We know this symbol has arg reloc bits.  */
  1690.   add_entry_to_symext_chain (abfd, args_hash->arg_bits,
  1691.                  index, &symext_rootP, &symext_lastP);
  1692.   return true;
  1693. }
  1694.  
  1695. /* Perform any processing needed late in the object file writing process.
  1696.    For PA ELF we build and set the contents of the symbol extension
  1697.    section.  */
  1698.  
  1699. static void
  1700. elf32_hppa_backend_final_write_processing (abfd, linker)
  1701.      bfd *abfd;
  1702.      boolean linker;
  1703. {
  1704.   asection *symextn_sec;
  1705.   unsigned int i, *symtab_map = (unsigned int *) elf_sym_extra (abfd);
  1706.  
  1707.   /* Now build the symbol extension section.  */
  1708.   if (symext_chain_size == 0)
  1709.     return;
  1710.  
  1711.   if (! linker)
  1712.     {
  1713.       /* We were not called from the backend linker, so we still need
  1714.      to build the symbol extension chain.
  1715.  
  1716.          Look at each symbol, adding the appropriate information to the
  1717.      symbol extension section list as necessary.  */
  1718.       for (i = 0; i < abfd->symcount; i++)
  1719.     {
  1720.       elf_symbol_type *symbol = (elf_symbol_type *) abfd->outsymbols[i];
  1721.  
  1722.       /* Only functions ever need an entry in the symbol extension
  1723.          section.  */
  1724.       if (!(symbol->symbol.flags & BSF_FUNCTION))
  1725.         continue;
  1726.  
  1727.       /* And only if they specify the locations of their arguments.  */
  1728.       if (symbol->tc_data.hppa_arg_reloc == 0)
  1729.         continue;
  1730.  
  1731.       /* Add this symbol's information to the chain.  */
  1732.       add_entry_to_symext_chain (abfd, symbol->tc_data.hppa_arg_reloc,
  1733.                      symtab_map[i], &symext_rootP,
  1734.                      &symext_lastP);
  1735.     }
  1736.     }
  1737.  
  1738.   /* Now fill in the contents of the symbol extension section.  */
  1739.   elf_hppa_tc_make_sections (abfd, symext_rootP);
  1740.  
  1741.   /* And attach that as the section's contents.  */
  1742.   symextn_sec = bfd_get_section_by_name (abfd, SYMEXTN_SECTION_NAME);
  1743.   if (symextn_sec == (asection *) 0)
  1744.     abort();
  1745.  
  1746.   symextn_sec->contents = (void *)symextn_contents;
  1747.  
  1748.   bfd_set_section_contents (abfd, symextn_sec, symextn_sec->contents,
  1749.                 symextn_sec->output_offset, symextn_sec->_raw_size);
  1750. }
  1751.  
  1752. /* Update the symbol extention chain to include the symbol pointed to
  1753.    by SYMBOLP if SYMBOLP is a function symbol.  Used internally and by GAS.  */
  1754.  
  1755. static void
  1756. add_entry_to_symext_chain (abfd, arg_reloc, sym_idx, symext_root, symext_last)
  1757.      bfd *abfd;
  1758.      unsigned int arg_reloc;
  1759.      unsigned int sym_idx;
  1760.      symext_chainS **symext_root;
  1761.      symext_chainS **symext_last;
  1762. {
  1763.   symext_chainS *symextP;
  1764.  
  1765.   /* Allocate memory and initialize this entry.  */
  1766.   symextP = (symext_chainS *) bfd_alloc (abfd, sizeof (symext_chainS) * 2);
  1767.   if (!symextP)
  1768.     {
  1769.       bfd_set_error (bfd_error_no_memory);
  1770.       abort();            /* FIXME */
  1771.     }
  1772.  
  1773.   symextP[0].entry = ELF32_PARISC_SX_WORD (PARISC_SXT_SYMNDX, sym_idx);
  1774.   symextP[0].next = &symextP[1];
  1775.  
  1776.   symextP[1].entry = ELF32_PARISC_SX_WORD (PARISC_SXT_ARG_RELOC, arg_reloc);
  1777.   symextP[1].next = NULL;
  1778.  
  1779.   /* Now update the chain itself so it can be walked later to build
  1780.      the symbol extension section.  */
  1781.   if (*symext_root == NULL)
  1782.     {
  1783.       *symext_root = &symextP[0];
  1784.       *symext_last = &symextP[1];
  1785.     }
  1786.   else
  1787.     {
  1788.       (*symext_last)->next = &symextP[0];
  1789.       *symext_last = &symextP[1];
  1790.     }
  1791. }
  1792.  
  1793. /* Build the symbol extension section.  */
  1794.  
  1795. static void
  1796. elf_hppa_tc_make_sections (abfd, symext_root)
  1797.      bfd *abfd;
  1798.      symext_chainS *symext_root;
  1799. {
  1800.   symext_chainS *symextP;
  1801.   unsigned int i;
  1802.   asection *symextn_sec;
  1803.  
  1804.   symextn_sec = bfd_get_section_by_name (abfd, SYMEXTN_SECTION_NAME);
  1805.  
  1806.   /* Grab some memory for the contents of the symbol extension section
  1807.      itself.  */
  1808.   symextn_contents = (symext_entryS *) bfd_zalloc (abfd,
  1809.                            symextn_sec->_raw_size);
  1810.   if (!symextn_contents)
  1811.     {
  1812.       bfd_set_error (bfd_error_no_memory);
  1813.       abort();            /* FIXME */
  1814.     }
  1815.  
  1816.   /* Fill in the contents of the symbol extension chain.  */
  1817.   for (i = 0, symextP = symext_root; symextP; symextP = symextP->next, ++i)
  1818.     symextn_contents[i] = symextP->entry;
  1819.  
  1820.   return;
  1821. }
  1822.  
  1823. /* Do some PA ELF specific work after reading in the symbol table.
  1824.    In particular attach the argument relocation from the
  1825.    symbol extension section to the appropriate symbols.  */
  1826.  
  1827. static boolean
  1828. elf32_hppa_backend_symbol_table_processing (abfd, esyms,symcnt)
  1829.      bfd *abfd;
  1830.      elf_symbol_type *esyms;
  1831.      int symcnt;
  1832. {
  1833.   Elf32_Internal_Shdr *symextn_hdr =
  1834.     bfd_elf_find_section (abfd, SYMEXTN_SECTION_NAME);
  1835.   unsigned int i, current_sym_idx = 0;
  1836.  
  1837.   /* If no symbol extension existed, then all symbol extension information
  1838.      is assumed to be zero.  */
  1839.   if (symextn_hdr == NULL)
  1840.     {
  1841.       for (i = 0; i < symcnt; i++)
  1842.     esyms[i].tc_data.hppa_arg_reloc = 0;
  1843.       return (true);
  1844.     }
  1845.  
  1846.   /* FIXME:  Why not use bfd_get_section_contents here?  Also should give
  1847.      memory back when we're done.  */
  1848.   /* Allocate a buffer of the appropriate size for the symextn section.  */
  1849.   symextn_hdr->contents = bfd_zalloc(abfd,symextn_hdr->sh_size);
  1850.   if (!symextn_hdr->contents)
  1851.     {
  1852.       bfd_set_error (bfd_error_no_memory);
  1853.       return false;
  1854.     }
  1855.   symextn_hdr->size = symextn_hdr->sh_size;
  1856.  
  1857.   /* Read in the symextn section.  */
  1858.   if (bfd_seek (abfd, symextn_hdr->sh_offset, SEEK_SET) == -1)
  1859.     return false;
  1860.   if (bfd_read ((PTR) symextn_hdr->contents, 1, symextn_hdr->size, abfd)
  1861.       != symextn_hdr->size)
  1862.     return false;
  1863.  
  1864.   /* Parse entries in the symbol extension section, updating the symtab
  1865.      entries as we go */
  1866.   for (i = 0; i < symextn_hdr->size / sizeof(symext_entryS); i++)
  1867.     {
  1868.       symext_entryS *seP = ((symext_entryS *)symextn_hdr->contents) + i;
  1869.       unsigned int se_value = ELF32_PARISC_SX_VAL (*seP);
  1870.       unsigned int se_type = ELF32_PARISC_SX_TYPE (*seP);
  1871.  
  1872.       switch (se_type)
  1873.     {
  1874.     case PARISC_SXT_NULL:
  1875.       break;
  1876.  
  1877.     case PARISC_SXT_SYMNDX:
  1878.       if (se_value >= symcnt)
  1879.         {
  1880.           bfd_set_error (bfd_error_bad_value);
  1881.           return (false);
  1882.         }
  1883.       current_sym_idx = se_value - 1;
  1884.       break;
  1885.  
  1886.     case PARISC_SXT_ARG_RELOC:
  1887.       esyms[current_sym_idx].tc_data.hppa_arg_reloc = se_value;
  1888.       break;
  1889.  
  1890.     default:
  1891.       bfd_set_error (bfd_error_bad_value);
  1892.       return (false);
  1893.     }
  1894.     }
  1895.   return (true);
  1896. }
  1897.  
  1898. /* Read and attach the symbol extension information for the symbols
  1899.    in INPUT_BFD to the argument location hash table.  Handle locals
  1900.    if DO_LOCALS is true; likewise for globals when DO_GLOBALS is true.  */
  1901.  
  1902. static boolean
  1903. elf32_hppa_read_symext_info (input_bfd, symtab_hdr, args_hash_table,
  1904.                  local_syms, do_locals, do_globals)
  1905.      bfd *input_bfd;
  1906.      Elf_Internal_Shdr *symtab_hdr;
  1907.      struct elf32_hppa_args_hash_table *args_hash_table;
  1908.      Elf_Internal_Sym *local_syms;
  1909.      boolean do_locals;
  1910.      boolean do_globals;
  1911. {
  1912.   asection *symextn_sec;
  1913.   symextn_entry *contents;
  1914.   unsigned int i, n_entries, current_index = 0;
  1915.  
  1916.   /* Get the symbol extension section for this BFD.  If no section exists
  1917.      then there's nothing to do.  Likewise if the section exists, but
  1918.      has no contents.  */
  1919.   symextn_sec = bfd_get_section_by_name (input_bfd, SYMEXTN_SECTION_NAME);
  1920.   if (symextn_sec == NULL)
  1921.     return true;
  1922.  
  1923.   /* Done separately so we can turn off SEC_HAS_CONTENTS (see below).  */
  1924.   if (symextn_sec->_raw_size == 0)
  1925.     {
  1926.       symextn_sec->flags &= ~SEC_HAS_CONTENTS;
  1927.       return true;
  1928.     }
  1929.  
  1930.   contents = (symextn_entry *) malloc (symextn_sec->_raw_size);
  1931.   if (contents == NULL)
  1932.     {
  1933.       bfd_set_error (bfd_error_no_memory);
  1934.       return false;
  1935.     }
  1936.  
  1937.   /* How gross.  We turn off SEC_HAS_CONTENTS for the input symbol extension
  1938.      sections to keep the generic ELF/BFD code from trying to do anything
  1939.      with them.  We have to undo that hack temporarily so that we can read
  1940.      in the contents with the generic code.  */
  1941.   symextn_sec->flags |= SEC_HAS_CONTENTS;
  1942.   if (bfd_get_section_contents (input_bfd, symextn_sec, contents,
  1943.                 0, symextn_sec->_raw_size) == false)
  1944.     {
  1945.       symextn_sec->flags &= ~SEC_HAS_CONTENTS;
  1946.       free (contents);
  1947.       return false;
  1948.     }
  1949.  
  1950.   /* Gross.  Turn off SEC_HAS_CONTENTS for the input symbol extension
  1951.      sections (see above).  */
  1952.   symextn_sec->flags &= ~SEC_HAS_CONTENTS;
  1953.  
  1954.   n_entries = symextn_sec->_raw_size / sizeof (symextn_entry);
  1955.   for (i = 0; i < n_entries; i++)
  1956.     {
  1957.       symextn_entry entry = contents[i];
  1958.       unsigned int value = ELF32_PARISC_SX_VAL (entry);
  1959.       unsigned int type = ELF32_PARISC_SX_TYPE (entry);
  1960.       struct elf32_hppa_args_hash_entry *args_hash;
  1961.  
  1962.       switch (type)
  1963.     {
  1964.     case PARISC_SXT_NULL:
  1965.       break;
  1966.  
  1967.     case PARISC_SXT_SYMNDX:
  1968.       if (value >= symtab_hdr->sh_size / sizeof (Elf32_External_Sym))
  1969.         {
  1970.           bfd_set_error (bfd_error_bad_value);
  1971.           free (contents);
  1972.           return false;
  1973.         }
  1974.       current_index = value;
  1975.       break;
  1976.  
  1977.     case PARISC_SXT_ARG_RELOC:
  1978.       if (current_index < symtab_hdr->sh_info
  1979.           && do_locals)
  1980.         {
  1981.           Elf_Internal_Shdr *hdr;
  1982.           char *new_name;
  1983.           const char *sym_name;
  1984.           asection *sym_sec;
  1985.           unsigned int len;
  1986.  
  1987.           hdr = elf_elfsections (input_bfd)[local_syms[current_index].st_shndx];
  1988.           sym_sec = (asection *)hdr->rawdata;
  1989.           sym_name = elf_string_from_elf_section (input_bfd,
  1990.                               symtab_hdr->sh_link,
  1991.                          local_syms[current_index].st_name);
  1992.           len = strlen (sym_name) + 10;
  1993.           new_name = malloc (len);
  1994.           if (new_name == NULL)
  1995.         {
  1996.           bfd_set_error (bfd_error_no_memory);
  1997.           free (contents);
  1998.           return false;
  1999.         }
  2000.           strcpy (new_name, sym_name);
  2001.           sprintf (new_name + len - 10, "_%08x", (int)sym_sec);
  2002.  
  2003.           /* This is a global symbol with argument location info.
  2004.          We need to enter it into the hash table.  */
  2005.           args_hash = elf32_hppa_args_hash_lookup (args_hash_table,
  2006.                                new_name, true,
  2007.                                true);
  2008.           free (new_name);
  2009.           if (args_hash == NULL)
  2010.         {
  2011.           free (contents);
  2012.           return false;
  2013.         }
  2014.           args_hash->arg_bits = value;
  2015.           break;
  2016.         }
  2017.       else if (current_index >= symtab_hdr->sh_info
  2018.            && do_globals)
  2019.         {
  2020.           struct elf_link_hash_entry *h;
  2021.  
  2022.           current_index -= symtab_hdr->sh_info;
  2023.           h = elf_sym_hashes(input_bfd)[current_index];
  2024.           /* This is a global symbol with argument location
  2025.          information.  We need to enter it into the hash table.  */
  2026.           args_hash = elf32_hppa_args_hash_lookup (args_hash_table,
  2027.                                h->root.root.string,
  2028.                                true, true);
  2029.           if (args_hash == NULL)
  2030.         {
  2031.           bfd_set_error (bfd_error_bad_value);
  2032.           free (contents);
  2033.           return false;
  2034.         }
  2035.           args_hash->arg_bits = value;
  2036.           break;
  2037.         }
  2038.       else
  2039.         break;
  2040.  
  2041.     default:
  2042.       bfd_set_error (bfd_error_bad_value);
  2043.       free (contents);
  2044.       return false;
  2045.     }
  2046.     }
  2047.   free (contents);
  2048.   return true;
  2049. }
  2050.  
  2051. /* Undo the generic ELF code's subtraction of section->vma from the
  2052.    value of each external symbol.  */
  2053.  
  2054. static boolean
  2055. elf32_hppa_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
  2056.      bfd *abfd;
  2057.      struct bfd_link_info *info;
  2058.      const Elf_Internal_Sym *sym;
  2059.      const char **namep;
  2060.      flagword *flagsp;
  2061.      asection **secp;
  2062.      bfd_vma *valp;
  2063. {
  2064.   *valp += (*secp)->vma;
  2065.   return true;
  2066. }
  2067.  
  2068. /* Determine the name of the stub needed to perform a call assuming the
  2069.    argument relocation bits for caller and callee are in CALLER and CALLEE
  2070.    for a call from LOCATION to DESTINATION.  Copy the name into STUB_NAME.  */
  2071.  
  2072. static void
  2073. elf32_hppa_name_of_stub (caller, callee, location, destination, stub_name)
  2074.      unsigned int caller, callee;
  2075.      bfd_vma location, destination;
  2076.      char *stub_name;
  2077. {
  2078.   arg_reloc_type arg_reloc_types[5];
  2079.  
  2080.   if (elf32_hppa_arg_reloc_needed (caller, callee, arg_reloc_types))
  2081.     {
  2082.       arg_reloc_location i;
  2083.       /* Fill in the basic template.  */
  2084.       strcpy (stub_name, "__XX_XX_XX_XX_XX_stub_");
  2085.  
  2086.       /* Now fix the specifics.  */
  2087.       for (i = ARG0; i <= RET; i++)
  2088.     switch (arg_reloc_types[i])
  2089.       {
  2090.         case NO:
  2091.           stub_name[3 * i + 2] = 'N';
  2092.           stub_name[3 * i + 3] = 'O';
  2093.           break;
  2094.         case GF:
  2095.           stub_name[3 * i + 2] = 'G';
  2096.           stub_name[3 * i + 3] = 'F';
  2097.           break;
  2098.         case FG:
  2099.           stub_name[3 * i + 2] = 'F';
  2100.           stub_name[3 * i + 3] = 'G';
  2101.           break;
  2102.         case GD:
  2103.           stub_name[3 * i + 2] = 'G';
  2104.           stub_name[3 * i + 3] = 'D';
  2105.           break;
  2106.         case DG:
  2107.           stub_name[3 * i + 2] = 'D';
  2108.           stub_name[3 * i + 3] = 'G';
  2109.           break;
  2110.       }
  2111.     }
  2112.   else
  2113.     strcpy (stub_name, "_____long_branch_stub_");
  2114. }
  2115.  
  2116. /* Determine if an argument relocation stub is needed to perform a
  2117.    call assuming the argument relocation bits for caller and callee
  2118.    are in CALLER and CALLEE.  Place the type of relocations (if any)
  2119.    into stub_types_p.  */
  2120.  
  2121. static boolean
  2122. elf32_hppa_arg_reloc_needed (caller, callee, stub_types)
  2123.      unsigned int caller, callee;
  2124.      arg_reloc_type stub_types[5];
  2125. {
  2126.   /* Special case for no relocations.  */
  2127.   if (caller == 0 || callee == 0)
  2128.     return 0;
  2129.   else
  2130.     {
  2131.       arg_location caller_loc[5];
  2132.       arg_location callee_loc[5];
  2133.  
  2134.       /* Extract the location information for the argument and return
  2135.      value on both the caller and callee sides.  */
  2136.       caller_loc[ARG0] = EXTRACT_ARBITS (caller, ARG0);
  2137.       callee_loc[ARG0] = EXTRACT_ARBITS (callee, ARG0);
  2138.       caller_loc[ARG1] = EXTRACT_ARBITS (caller, ARG1);
  2139.       callee_loc[ARG1] = EXTRACT_ARBITS (callee, ARG1);
  2140.       caller_loc[ARG2] = EXTRACT_ARBITS (caller, ARG2);
  2141.       callee_loc[ARG2] = EXTRACT_ARBITS (callee, ARG2);
  2142.       caller_loc[ARG3] = EXTRACT_ARBITS (caller, ARG3);
  2143.       callee_loc[ARG3] = EXTRACT_ARBITS (callee, ARG3);
  2144.       caller_loc[RET] = EXTRACT_ARBITS (caller, RET);
  2145.       callee_loc[RET] = EXTRACT_ARBITS (callee, RET);
  2146.  
  2147.       /* Check some special combinations.  This is necessary to
  2148.      deal with double precision FP arguments.  */
  2149.       if (caller_loc[ARG0] == AR_FU || caller_loc[ARG1] == AR_FU)
  2150.     {
  2151.       caller_loc[ARG0] = AR_FPDBL1;
  2152.       caller_loc[ARG1] = AR_NO;
  2153.     }
  2154.       if (caller_loc[ARG2] == AR_FU || caller_loc[ARG3] == AR_FU)
  2155.     {
  2156.       caller_loc[ARG2] = AR_FPDBL2;
  2157.       caller_loc[ARG3] = AR_NO;
  2158.     }
  2159.       if (callee_loc[ARG0] == AR_FU || callee_loc[ARG1] == AR_FU)
  2160.     {
  2161.       callee_loc[ARG0] = AR_FPDBL1;
  2162.       callee_loc[ARG1] = AR_NO;
  2163.     }
  2164.       if (callee_loc[ARG2] == AR_FU || callee_loc[ARG3] == AR_FU)
  2165.     {
  2166.       callee_loc[ARG2] = AR_FPDBL2;
  2167.       callee_loc[ARG3] = AR_NO;
  2168.     }
  2169.  
  2170.       /* Now look up any relocation needed for each argument and the
  2171.      return value.  */
  2172.       stub_types[ARG0] = arg_mismatches[caller_loc[ARG0]][callee_loc[ARG0]];
  2173.       stub_types[ARG1] = arg_mismatches[caller_loc[ARG1]][callee_loc[ARG1]];
  2174.       stub_types[ARG2] = arg_mismatches[caller_loc[ARG2]][callee_loc[ARG2]];
  2175.       stub_types[ARG3] = arg_mismatches[caller_loc[ARG3]][callee_loc[ARG3]];
  2176.       stub_types[RET] = ret_mismatches[caller_loc[RET]][callee_loc[RET]];
  2177.  
  2178.       return (stub_types[ARG0] != NO
  2179.           || stub_types[ARG1] != NO
  2180.           || stub_types[ARG2] != NO
  2181.           || stub_types[ARG3] != NO
  2182.           || stub_types[RET] != NO);
  2183.     }
  2184. }
  2185.  
  2186. /* Compute the size of the stub needed to call from LOCATION to DESTINATION
  2187.    (a function named SYM_NAME), with argument relocation bits CALLER and
  2188.    CALLEE.  Return zero if no stub is needed to perform such a call.  */
  2189.  
  2190. static unsigned int
  2191. elf32_hppa_size_of_stub (callee, caller, location, destination, sym_name)
  2192.      unsigned int callee, caller;
  2193.      bfd_vma location, destination;
  2194.      const char *sym_name;
  2195. {
  2196.   arg_reloc_type arg_reloc_types[5];
  2197.  
  2198.   /* Determine if a long branch or argument relocation stub is needed.
  2199.      If an argument relocation stub is needed, the relocation will be
  2200.      stored into arg_reloc_types.  */
  2201.   if (!(((int)(location - destination) > 0x3ffff)
  2202.     || ((int)(location - destination) < (int)0xfffc0000)
  2203.     || elf32_hppa_arg_reloc_needed (caller, callee, arg_reloc_types)))
  2204.     return 0;
  2205.  
  2206.   /* Some kind of stub is needed.  Determine how big it needs to be.
  2207.      First check for argument relocation stubs as they also handle
  2208.      long calls.  Then check for long calls to millicode and finally
  2209.      the normal long calls.  */
  2210.   if (arg_reloc_types[ARG0] != NO
  2211.       || arg_reloc_types[ARG1] != NO
  2212.       || arg_reloc_types[ARG2] != NO
  2213.       || arg_reloc_types[ARG3] != NO
  2214.       || arg_reloc_types[RET] != NO)
  2215.     {
  2216.       /* Some kind of argument relocation stub is needed.  */
  2217.       unsigned int len = 16;
  2218.       arg_reloc_location i;
  2219.  
  2220.       /* Each GR or FG relocation takes 2 insns, each GD or DG
  2221.      relocation takes 3 insns.  Plus 4 more insns for the
  2222.          RP adjustment, ldil & (be | ble) and copy.  */
  2223.       for (i = ARG0; i <= RET; i++)
  2224.     switch (arg_reloc_types[i])
  2225.       {
  2226.         case GF:
  2227.         case FG:
  2228.           len += 8;
  2229.           break;
  2230.  
  2231.         case GD:
  2232.         case DG:
  2233.           len += 12;
  2234.           break;
  2235.  
  2236.         default:
  2237.           break;
  2238.       }
  2239.  
  2240.       /* Extra instructions are needed if we're relocating a return value.  */
  2241.       if (arg_reloc_types[RET] != NO)
  2242.     len += 12;
  2243.  
  2244.       return len;
  2245.     }
  2246.   else if (!strncmp ("$$", sym_name, 2)
  2247.       && strcmp ("$$dyncall", sym_name))
  2248.     return 12;
  2249.   else
  2250.     return 16;
  2251. }
  2252.  
  2253. /* Build one linker stub as defined by the stub hash table entry GEN_ENTRY.
  2254.    IN_ARGS contains the stub BFD and link info pointers.  */
  2255.  
  2256. static boolean
  2257. elf32_hppa_build_one_stub (gen_entry, in_args)
  2258.      struct bfd_hash_entry *gen_entry;
  2259.      PTR in_args;
  2260. {
  2261.   void **args = (void **)in_args;
  2262.   bfd *stub_bfd = (bfd *)args[0];
  2263.   struct bfd_link_info *info = (struct bfd_link_info *)args[1];
  2264.   struct elf32_hppa_stub_hash_entry *entry;
  2265.   struct elf32_hppa_stub_hash_table *stub_hash_table;
  2266.   bfd_byte *loc;
  2267.   symvalue sym_value;
  2268.   const char *sym_name;
  2269.  
  2270.   /* Initialize pointers to the stub hash table, the particular entry we
  2271.      are building a stub for, and where (in memory) we should place the stub
  2272.      instructions.  */
  2273.   entry = (struct elf32_hppa_stub_hash_entry *)gen_entry;
  2274.   stub_hash_table = elf32_hppa_hash_table(info)->stub_hash_table;
  2275.   loc = stub_hash_table->location;
  2276.  
  2277.   /* Make a note of the offset within the stubs for this entry.  */
  2278.   entry->offset = stub_hash_table->offset;
  2279.  
  2280.   /* The symbol's name starts at offset 22.  */
  2281.   sym_name = entry->root.string + 22;
  2282.  
  2283.   sym_value = (entry->target_value
  2284.            + entry->target_section->output_offset
  2285.            + entry->target_section->output_section->vma);
  2286.  
  2287.   if (strncmp ("_____long_branch_stub_", entry->root.string, 22))
  2288.     {
  2289.       /* This must be an argument or return value relocation stub.  */
  2290.       unsigned long insn;
  2291.       arg_reloc_location i;
  2292.       bfd_byte *begin_loc = loc;
  2293.  
  2294.       /* First the return pointer adjustment.  Depending on exact calling
  2295.      sequence this instruction may be skipped.  */
  2296.       bfd_put_32 (stub_bfd, LDO_M4_R31_R31, loc);
  2297.       loc += 4;
  2298.  
  2299.       /* If we are relocating a return value, then we're going to have
  2300.      to return into the stub.  So we have to save off the user's
  2301.      return pointer into the stack at RP'.  */
  2302.       if (strncmp (entry->root.string + 14, "NO", 2))
  2303.     {
  2304.       bfd_put_32 (stub_bfd, STW_R31_M8R30, loc);
  2305.       loc += 4;
  2306.     }
  2307.  
  2308.       /* Iterate over the argument relocations, emitting instructions
  2309.      to move them around as necessary.  */
  2310.       for (i = ARG0; i <= ARG3; i++)
  2311.     {
  2312.       if (!strncmp (entry->root.string + 3 * i + 2, "GF", 2))
  2313.         {
  2314.           bfd_put_32 (stub_bfd, STW_ARG_M16R30 | ((26 - i) << 16), loc);
  2315.           bfd_put_32 (stub_bfd, FLDW_M16R30_FARG | (4 + i), loc + 4);
  2316.           loc += 8;
  2317.         }
  2318.       else if (!strncmp (entry->root.string + 3 * i + 2, "FG", 2))
  2319.         {
  2320.           bfd_put_32 (stub_bfd, FSTW_FARG_M16R30 | (4 + i), loc);
  2321.           bfd_put_32 (stub_bfd, LDW_M16R30_ARG | ((26 - i) << 16), loc + 4);
  2322.           loc += 8;
  2323.         }
  2324.       else if (!strncmp (entry->root.string + 3 * i + 2, "GD", 2))
  2325.         {
  2326.           bfd_put_32 (stub_bfd, STW_ARG_M12R30 | ((26 - i) << 16), loc);
  2327.           bfd_put_32 (stub_bfd, STW_ARG_M16R30 | ((25 - i) << 16), loc + 4);
  2328.           bfd_put_32 (stub_bfd, FLDD_M16R30_FARG | (5 + i), loc + 8);
  2329.           loc += 12;
  2330.         }
  2331.       else if (!strncmp (entry->root.string + 3 * i + 2, "DG", 2))
  2332.         {
  2333.           bfd_put_32 (stub_bfd, FSTD_FARG_M16R30 | (5 + i), loc);
  2334.           bfd_put_32 (stub_bfd, LDW_M12R30_ARG | ((26 - i) << 16), loc + 4);
  2335.           bfd_put_32 (stub_bfd, LDW_M16R30_ARG | ((25 - i) << 16), loc + 8);
  2336.           loc += 12;
  2337.         }
  2338.     }
  2339.  
  2340.       /* Load the high bits of the target address into %r1.  */
  2341.       insn = hppa_rebuild_insn (stub_bfd, LDIL_R1,
  2342.                 hppa_field_adjust (sym_value, 0, e_lrsel), 21);
  2343.       bfd_put_32 (stub_bfd, insn, loc);
  2344.       loc += 4;
  2345.  
  2346.       /* If we are relocating a return value, then we're going to have
  2347.      to return into the stub, then perform the return value relocation.  */
  2348.       if (strncmp (entry->root.string + 14, "NO", 2))
  2349.     {
  2350.       /* To return to the stub we "ble" to the target and copy the return
  2351.          pointer from %r31 into %r2.  */
  2352.       insn = hppa_rebuild_insn (stub_bfd,
  2353.                     BLE_SR4_R1,
  2354.                     hppa_field_adjust (sym_value, 0,
  2355.                                e_rrsel) >> 2,
  2356.                     17);
  2357.       bfd_put_32 (stub_bfd, insn, loc);
  2358.       bfd_put_32 (stub_bfd, COPY_R31_R2, loc + 4);
  2359.  
  2360.       /* Reload the return pointer for our caller from the stack.  */
  2361.       bfd_put_32 (stub_bfd, LDW_M8R30_R31, loc + 8);
  2362.       loc += 12;
  2363.  
  2364.       /* Perform the return value relocation.  */
  2365.       if (!strncmp (entry->root.string + 14, "GF", 2))
  2366.         {
  2367.           bfd_put_32 (stub_bfd, STW_ARG_M16R30 | (28 << 16), loc);
  2368.           bfd_put_32 (stub_bfd, FLDW_M16R30_FARG | 4, loc + 4);
  2369.           loc += 8;
  2370.         }
  2371.       else if (!strncmp (entry->root.string + 14, "FG", 2))
  2372.         {
  2373.           bfd_put_32 (stub_bfd, FSTW_FARG_M16R30 | 4, loc);
  2374.           bfd_put_32 (stub_bfd, LDW_M16R30_ARG | (28 << 16), loc + 4);
  2375.           loc += 8;
  2376.         }
  2377.       else if (!strncmp (entry->root.string + 2, "GD", 2))
  2378.         {
  2379.           bfd_put_32 (stub_bfd, STW_ARG_M12R30 | (28 << 16), loc);
  2380.           bfd_put_32 (stub_bfd, STW_ARG_M16R30 | (29 << 16), loc + 4);
  2381.           bfd_put_32 (stub_bfd, FLDD_M16R30_FARG | 4, loc + 8);
  2382.           loc += 12;
  2383.         }
  2384.       else if (!strncmp (entry->root.string + 2, "DG", 2))
  2385.         {
  2386.           bfd_put_32 (stub_bfd, FSTD_FARG_M16R30 | 4, loc);
  2387.           bfd_put_32 (stub_bfd, LDW_M12R30_ARG | (28 << 16), loc + 4);
  2388.           bfd_put_32 (stub_bfd, LDW_M16R30_ARG | (29 << 16), loc + 8);
  2389.           loc += 12;
  2390.         }
  2391.       /* Branch back to the user's code now.  */
  2392.       bfd_put_32 (stub_bfd, BV_N_0_R31, loc);
  2393.       loc += 4;
  2394.     }
  2395.       else
  2396.     {
  2397.       /* No return value relocation, so we can simply "be" to the
  2398.          target and copy out return pointer into %r2.  */
  2399.       insn = hppa_rebuild_insn (stub_bfd, BE_SR4_R1,
  2400.                     hppa_field_adjust (sym_value, 0,
  2401.                                e_rrsel) >> 2, 17);
  2402.       bfd_put_32 (stub_bfd, insn, loc);
  2403.       bfd_put_32 (stub_bfd, COPY_R31_R2, loc + 4);
  2404.       loc += 8;
  2405.     }
  2406.  
  2407.       /* Update the location and offsets.  */
  2408.       stub_hash_table->location += (loc - begin_loc);
  2409.       stub_hash_table->offset += (loc - begin_loc);
  2410.     }
  2411.   else
  2412.     {
  2413.       /* Create one of two variant long branch stubs.  One for $$dyncall and
  2414.      normal calls, the other for calls to millicode.  */
  2415.       unsigned long insn;
  2416.       int millicode_call = 0;
  2417.  
  2418.       if (!strncmp ("$$", sym_name, 2) && strcmp ("$$dyncall", sym_name))
  2419.     millicode_call = 1;
  2420.  
  2421.       /* First the return pointer adjustment.  Depending on exact calling
  2422.      sequence this instruction may be skipped.  */
  2423.       bfd_put_32 (stub_bfd, LDO_M4_R31_R31, loc);
  2424.  
  2425.       /* The next two instructions are the long branch itself.  A long branch
  2426.      is formed with "ldil" loading the upper bits of the target address
  2427.      into a register, then branching with "be" which adds in the lower bits.
  2428.      Long branches to millicode nullify the delay slot of the "be".  */
  2429.       insn = hppa_rebuild_insn (stub_bfd, LDIL_R1,
  2430.                 hppa_field_adjust (sym_value, 0, e_lrsel), 21);
  2431.       bfd_put_32 (stub_bfd, insn, loc + 4);
  2432.       insn = hppa_rebuild_insn (stub_bfd, BE_SR4_R1 | (millicode_call ? 2 : 0),
  2433.                 hppa_field_adjust (sym_value, 0, e_rrsel) >> 2,
  2434.                 17);
  2435.       bfd_put_32 (stub_bfd, insn, loc + 8);
  2436.  
  2437.       if (!millicode_call)
  2438.     {
  2439.       /* The sequence to call this stub places the return pointer into %r31,
  2440.          the final target expects the return pointer in %r2, so copy the
  2441.           return pointer into the proper register.  */
  2442.       bfd_put_32 (stub_bfd, COPY_R31_R2, loc + 12);
  2443.  
  2444.       /* Update the location and offsets.  */
  2445.       stub_hash_table->location += 16;
  2446.       stub_hash_table->offset += 16;
  2447.     }
  2448.       else
  2449.     {
  2450.       /* Update the location and offsets.  */
  2451.       stub_hash_table->location += 12;
  2452.       stub_hash_table->offset += 12;
  2453.     }
  2454.  
  2455.     }
  2456.   return true;
  2457. }
  2458.  
  2459. /* External entry points for sizing and building linker stubs.  */
  2460.  
  2461. /* Build all the stubs associated with the current output file.  The
  2462.    stubs are kept in a hash table attached to the main linker hash
  2463.    table.  This is called via hppaelf_finish in the linker.  */
  2464.  
  2465. boolean
  2466. elf32_hppa_build_stubs (stub_bfd, info)
  2467.      bfd *stub_bfd;
  2468.      struct bfd_link_info *info;
  2469. {
  2470.   /* The stub BFD only has one section.  */
  2471.   asection *stub_sec = stub_bfd->sections;
  2472.   struct elf32_hppa_stub_hash_table *table;
  2473.   unsigned int size;
  2474.   void *args[2];
  2475.  
  2476.   /* So we can pass both the BFD for the stubs and the link info
  2477.      structure to the routine which actually builds stubs.  */
  2478.   args[0] = stub_bfd;
  2479.   args[1] = info;
  2480.  
  2481.   /* Allocate memory to hold the linker stubs.  */
  2482.   size = bfd_section_size (stub_bfd, stub_sec);
  2483.   stub_sec->contents = (unsigned char *) bfd_zalloc (stub_bfd, size);
  2484.   if (stub_sec->contents == NULL)
  2485.     {
  2486.       bfd_set_error (bfd_error_no_memory);
  2487.       return false;
  2488.     }
  2489.   table = elf32_hppa_hash_table(info)->stub_hash_table;
  2490.   table->location = stub_sec->contents;
  2491.  
  2492.   /* Build the stubs as directed by the stub hash table.  */
  2493.   elf32_hppa_stub_hash_traverse (table, elf32_hppa_build_one_stub, args);
  2494.  
  2495.   return true;
  2496. }
  2497.  
  2498. /* Determine and set the size of the stub section for a final link.
  2499.  
  2500.    The basic idea here is to examine all the relocations looking for
  2501.    PC-relative calls to a target that is unreachable with a "bl"
  2502.    instruction or calls where the caller and callee disagree on the
  2503.    location of their arguments or return value.  */
  2504.  
  2505. boolean
  2506. elf32_hppa_size_stubs (stub_bfd, output_bfd, link_info)
  2507.      bfd *stub_bfd;
  2508.      bfd *output_bfd;
  2509.      struct bfd_link_info *link_info;
  2510. {
  2511.   bfd *input_bfd;
  2512.   asection *section, *stub_sec;
  2513.   Elf_Internal_Shdr *symtab_hdr;
  2514.   Elf_Internal_Sym *local_syms, *isym;
  2515.   Elf32_External_Sym *ext_syms, *esym;
  2516.   struct elf32_hppa_stub_hash_table *stub_hash_table = 0;
  2517.   struct elf32_hppa_args_hash_table *args_hash_table = 0;
  2518.  
  2519.   /* Create and initialize the stub hash table.  */
  2520.   stub_hash_table = ((struct elf32_hppa_stub_hash_table *)
  2521.              malloc (sizeof (struct elf32_hppa_stub_hash_table)));
  2522.   if (!stub_hash_table)
  2523.     {
  2524.       bfd_set_error (bfd_error_no_memory);
  2525.       goto error_return;
  2526.     }
  2527.  
  2528.   if (!elf32_hppa_stub_hash_table_init (stub_hash_table, stub_bfd,
  2529.                     elf32_hppa_stub_hash_newfunc))
  2530.     goto error_return;
  2531.  
  2532.   /* Likewise for the argument location hash table.  */
  2533.   args_hash_table = ((struct elf32_hppa_args_hash_table *)
  2534.              malloc (sizeof (struct elf32_hppa_args_hash_table)));
  2535.   if (!args_hash_table)
  2536.     {
  2537.       bfd_set_error (bfd_error_no_memory);
  2538.       goto error_return;
  2539.     }
  2540.  
  2541.   if (!elf32_hppa_args_hash_table_init (args_hash_table,
  2542.                     elf32_hppa_args_hash_newfunc))
  2543.     goto error_return;
  2544.  
  2545.   /* Attach the hash tables to the main hash table.  */
  2546.   elf32_hppa_hash_table(link_info)->stub_hash_table = stub_hash_table;
  2547.   elf32_hppa_hash_table(link_info)->args_hash_table = args_hash_table;
  2548.  
  2549.   /* Walk over all the input BFDs adding entries to the args hash table
  2550.      for all the external functions.  */
  2551.   for (input_bfd = link_info->input_bfds;
  2552.        input_bfd != NULL;
  2553.        input_bfd = input_bfd->link_next)
  2554.     {
  2555.       /* We'll need the symbol table in a second.  */
  2556.       symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  2557.       if (symtab_hdr->sh_info == 0)
  2558.     continue;
  2559.  
  2560.       if (elf32_hppa_read_symext_info (input_bfd, symtab_hdr, args_hash_table,
  2561.                        NULL, false, true) == false)
  2562.     goto error_return;
  2563.     }
  2564.  
  2565.   /* Magic as we know the stub bfd only has one section.  */
  2566.   stub_sec = stub_bfd->sections;
  2567.  
  2568.   /* Now that we have argument location information for all the global
  2569.      functions we can start looking for stubs.  */
  2570.   for (input_bfd = link_info->input_bfds;
  2571.        input_bfd != NULL;
  2572.        input_bfd = input_bfd->link_next)
  2573.     {
  2574.       unsigned int i;
  2575.  
  2576.       /* We'll need the symbol table in a second.  */
  2577.       symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  2578.       if (symtab_hdr->sh_info == 0)
  2579.     continue;
  2580.  
  2581.       /* We need an array of the local symbols attached to the input bfd.
  2582.      Unfortunately, we're going to have to read & swap them in.  */
  2583.       local_syms
  2584.     = (Elf_Internal_Sym *)malloc (symtab_hdr->sh_info
  2585.                       * sizeof (Elf_Internal_Sym));
  2586.       if (local_syms == NULL)
  2587.     {
  2588.       bfd_set_error (bfd_error_no_memory);
  2589.       goto error_return;
  2590.     }
  2591.  
  2592.       ext_syms
  2593.     = (Elf32_External_Sym *)malloc (symtab_hdr->sh_info
  2594.                     * sizeof (Elf32_External_Sym));
  2595.       if (ext_syms == NULL)
  2596.     {
  2597.       bfd_set_error (bfd_error_no_memory);
  2598.       free (local_syms);
  2599.       goto error_return;
  2600.     }
  2601.  
  2602.       if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
  2603.       || bfd_read (ext_syms, 1,
  2604.                (symtab_hdr->sh_info
  2605.             * sizeof (Elf32_External_Sym)), input_bfd)
  2606.       != (symtab_hdr->sh_info * sizeof (Elf32_External_Sym)))
  2607.     {
  2608.       free (local_syms);
  2609.       free (ext_syms);
  2610.       goto error_return;
  2611.     }
  2612.  
  2613.       /* Swap the local symbols in.  */
  2614.       isym = local_syms;
  2615.       esym = ext_syms;
  2616.       for (i = 0; i < symtab_hdr->sh_info; i++, esym++, isym++)
  2617.      bfd_elf32_swap_symbol_in (input_bfd, esym, isym);
  2618.  
  2619.       /* Now we can free the external symbols.  */
  2620.       free (ext_syms);
  2621.  
  2622.       if (elf32_hppa_read_symext_info (input_bfd, symtab_hdr, args_hash_table,
  2623.                        local_syms, true, false) == false)
  2624.     {
  2625.       free (local_syms);
  2626.       goto error_return;
  2627.     }
  2628.  
  2629.       /* If generating a relocateable output file, then we don't
  2630.      have to examine the relocs.  */
  2631.       if (link_info->relocateable)
  2632.     {
  2633.       free (local_syms);
  2634.       return true;
  2635.     }
  2636.  
  2637.       /* Walk over each section attached to the input bfd.  */
  2638.       for (section = input_bfd->sections;
  2639.        section != NULL;
  2640.        section = section->next)
  2641.     {
  2642.       Elf_Internal_Shdr *input_rel_hdr;
  2643.       Elf32_External_Rela *external_relocs, *erelaend, *erela;
  2644.       Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
  2645.  
  2646.       /* If there aren't any relocs, then there's nothing to do.  */
  2647.       if ((section->flags & SEC_RELOC) == 0
  2648.           || section->reloc_count == 0)
  2649.         continue;
  2650.  
  2651.       /* Allocate space for the external relocations.  */
  2652.       external_relocs
  2653.         = (Elf32_External_Rela *) malloc (section->reloc_count * sizeof (Elf32_External_Rela));
  2654.       if (external_relocs == NULL)
  2655.         {
  2656.           bfd_set_error (bfd_error_no_memory);
  2657.           free (local_syms);
  2658.           goto error_return;
  2659.         }
  2660.  
  2661.       /* Likewise for the internal relocations.  */
  2662.       internal_relocs
  2663.         = (Elf_Internal_Rela *) malloc (section->reloc_count * sizeof (Elf_Internal_Rela));
  2664.       if (internal_relocs == NULL)
  2665.         {
  2666.           bfd_set_error (bfd_error_no_memory);
  2667.           free (external_relocs);
  2668.           free (local_syms);
  2669.           goto error_return;
  2670.         }
  2671.  
  2672.       /* Read in the external relocs.  */
  2673.       input_rel_hdr = &elf_section_data (section)->rel_hdr;
  2674.       if (bfd_seek (input_bfd, input_rel_hdr->sh_offset, SEEK_SET) != 0
  2675.           || bfd_read (external_relocs, 1, input_rel_hdr->sh_size,
  2676.                input_bfd) != input_rel_hdr->sh_size)
  2677.         {
  2678.           free (external_relocs);
  2679.           free (internal_relocs);
  2680.           free (local_syms);
  2681.           goto error_return;
  2682.         }
  2683.  
  2684.       /* Swap in the relocs.  */
  2685.       erela = external_relocs;
  2686.       erelaend = erela + section->reloc_count;
  2687.       irela = internal_relocs;
  2688.       for (; erela < erelaend; erela++, irela++)
  2689.         bfd_elf32_swap_reloca_in (input_bfd, erela, irela);
  2690.  
  2691.       /* We're done with the external relocs, free them.  */
  2692.       free (external_relocs);
  2693.  
  2694.       /* Now examine each relocation.  */
  2695.       irela = internal_relocs;
  2696.       irelaend = irela + section->reloc_count;
  2697.       for (; irela < irelaend; irela++)
  2698.         {
  2699.           long r_type, callee_args, caller_args, r_index, size_of_stub;
  2700.           struct elf_link_hash_entry *hash;
  2701.           struct elf32_hppa_stub_hash_entry *stub_hash;
  2702.           struct elf32_hppa_args_hash_entry *args_hash;
  2703.           Elf_Internal_Sym *sym;
  2704.           asection *sym_sec;
  2705.           const char *sym_name;
  2706.           symvalue sym_value;
  2707.           bfd_vma location, destination;
  2708.           char *new_name = NULL;
  2709.  
  2710.           r_type = ELF32_R_TYPE (irela->r_info);
  2711.           r_index = ELF32_R_SYM (irela->r_info);
  2712.  
  2713.           if (r_type < 0 || r_type >= (int) R_PARISC_UNIMPLEMENTED)
  2714.         {
  2715.           bfd_set_error (bfd_error_bad_value);
  2716.           free (internal_relocs);
  2717.           free (local_syms);
  2718.           goto error_return;
  2719.         }
  2720.  
  2721.           /* Only look for stubs on call instructions or plabel
  2722.          references.  */
  2723.           if (r_type != R_PARISC_PCREL17F
  2724.           && r_type != R_PARISC_PLABEL32
  2725.           && r_type != R_PARISC_PLABEL21L
  2726.           && r_type != R_PARISC_PLABEL14R)
  2727.         continue;
  2728.  
  2729.           /* Now determine the call target, its name, value, section
  2730.          and argument relocation bits.  */
  2731.           hash = NULL;
  2732.           sym = NULL;
  2733.           sym_sec = NULL;
  2734.           if (r_index < symtab_hdr->sh_info)
  2735.         {
  2736.           /* It's a local symbol.  */
  2737.           Elf_Internal_Shdr *hdr;
  2738.  
  2739.           sym = local_syms + r_index;
  2740.           hdr = elf_elfsections (input_bfd)[sym->st_shndx];
  2741.           sym_sec = (asection *)hdr->rawdata;
  2742.           sym_name = elf_string_from_elf_section (input_bfd,
  2743.                               symtab_hdr->sh_link,
  2744.                               sym->st_name);
  2745.           sym_value = (ELF_ST_TYPE (sym->st_info) == STT_SECTION
  2746.                    ? 0 : sym->st_value);
  2747.           destination = (sym_value
  2748.                  + sym_sec->output_offset
  2749.                  + sym_sec->output_section->vma);
  2750.  
  2751.           /* Tack on an ID so we can uniquely identify this local
  2752.              symbol in the stub or arg info hash tables.  */
  2753.           new_name = malloc (strlen (sym_name) + 10);
  2754.           if (new_name == 0)
  2755.             {
  2756.               bfd_set_error (bfd_error_bad_value);
  2757.               free (internal_relocs);
  2758.               free (local_syms);
  2759.               goto error_return;
  2760.             }
  2761.           sprintf (new_name, "%s_%08x", sym_name, (int)sym_sec);
  2762.           sym_name = new_name;
  2763.         }
  2764.           else
  2765.         {
  2766.           /* It's an external symbol.  */
  2767.           long index;
  2768.  
  2769.           index = r_index - symtab_hdr->sh_info;
  2770.           hash = elf_sym_hashes (input_bfd)[index];
  2771.           if (hash->root.type == bfd_link_hash_defined)
  2772.             {
  2773.               sym_sec = hash->root.u.def.section;
  2774.               sym_name = hash->root.root.string;
  2775.               sym_value = hash->root.u.def.value;
  2776.               destination = (sym_value
  2777.                      + sym_sec->output_offset
  2778.                      + sym_sec->output_section->vma);
  2779.             }
  2780.           else
  2781.             {
  2782.               bfd_set_error (bfd_error_bad_value);
  2783.               free (internal_relocs);
  2784.               free (local_syms);
  2785.               goto error_return;
  2786.             }
  2787.         }
  2788.  
  2789.           args_hash = elf32_hppa_args_hash_lookup (args_hash_table,
  2790.                                sym_name, false, false);
  2791.  
  2792.           /* Get both caller and callee argument information.  */
  2793.           if (args_hash == NULL)
  2794.         callee_args = 0;
  2795.           else
  2796.         callee_args = args_hash->arg_bits;
  2797.  
  2798.           /* For calls get the caller's bits from the addend of
  2799.          the call relocation.  For PLABELS the caller's bits
  2800.          are assumed to have all args & return values in general
  2801.          registers (0x155).  */
  2802.           if (r_type == R_PARISC_PCREL17F)
  2803.         caller_args = HPPA_R_ARG_RELOC (irela->r_addend);
  2804.           else
  2805.         caller_args = 0x155;
  2806.  
  2807.           /* Now determine where the call point is.  */
  2808.           location = (section->output_offset
  2809.               + section->output_section->vma
  2810.               + irela->r_offset);
  2811.  
  2812.           /* We only care about the destination for PCREL function
  2813.          calls (eg. we don't care for PLABELS).  */
  2814.           if (r_type != R_PARISC_PCREL17F)
  2815.         location = destination;
  2816.  
  2817.           /* Determine what (if any) linker stub is needed and its
  2818.          size (in bytes).  */
  2819.           size_of_stub = elf32_hppa_size_of_stub (callee_args,
  2820.                               caller_args,
  2821.                               location,
  2822.                               destination,
  2823.                               sym_name);
  2824.           if (size_of_stub != 0)
  2825.         {
  2826.           char *stub_name;
  2827.           unsigned int len;
  2828.  
  2829.           /* Get the name of this stub.  */
  2830.           len = strlen (sym_name);
  2831.           len += 23;
  2832.  
  2833.           stub_name = malloc (len);
  2834.           if (!stub_name)
  2835.             {
  2836.               bfd_set_error (bfd_error_no_memory);
  2837.  
  2838.               /* Because sym_name was mallocd above for local
  2839.              symbols.  */
  2840.               if (r_index < symtab_hdr->sh_info)
  2841.             free (new_name);
  2842.  
  2843.               free (internal_relocs);
  2844.               free (local_syms);
  2845.               goto error_return;
  2846.             }
  2847.           elf32_hppa_name_of_stub (caller_args, callee_args,
  2848.                        location, destination, stub_name);
  2849.           strcat (stub_name + 22, sym_name);
  2850.  
  2851.           /* Because sym_name was malloced above for local symbols.  */
  2852.           if (r_index < symtab_hdr->sh_info)
  2853.             free (new_name);
  2854.  
  2855.           stub_hash
  2856.             = elf32_hppa_stub_hash_lookup (stub_hash_table, stub_name,
  2857.                            false, false);
  2858.           if (stub_hash != NULL)
  2859.             {
  2860.               /* The proper stub has already been created, nothing
  2861.              else to do.  */
  2862.               free (stub_name);
  2863.             }
  2864.           else
  2865.             {
  2866.               bfd_set_section_size (stub_bfd, stub_sec,
  2867.                         (bfd_section_size (stub_bfd,
  2868.                                    stub_sec)
  2869.                          + size_of_stub));
  2870.  
  2871.               /* Enter this entry into the linker stub hash table.  */
  2872.               stub_hash
  2873.             = elf32_hppa_stub_hash_lookup (stub_hash_table,
  2874.                                stub_name, true, true);
  2875.               if (stub_hash == NULL)
  2876.             {
  2877.               bfd_set_error (bfd_error_no_memory);
  2878.               free (stub_name);
  2879.               free (internal_relocs);
  2880.               free (local_syms);
  2881.               goto error_return;
  2882.             }
  2883.  
  2884.               /* We'll need these to determine the address that the
  2885.              stub will branch to.  */
  2886.               stub_hash->target_value = sym_value;
  2887.               stub_hash->target_section = sym_sec;
  2888.             }
  2889.           free (stub_name);
  2890.         }
  2891.         }
  2892.       /* We're done with the internal relocs, free them.  */
  2893.       free (internal_relocs);
  2894.     }
  2895.       /* We're done with the local symbols, free them.  */
  2896.       free (local_syms);
  2897.     }
  2898.   return true;
  2899.  
  2900. error_return:
  2901.   /* Return gracefully, avoiding dangling references to the hash tables.  */
  2902.   if (stub_hash_table)
  2903.     {
  2904.       elf32_hppa_hash_table(link_info)->stub_hash_table = NULL;
  2905.       free (stub_hash_table);
  2906.     }
  2907.   if (args_hash_table)
  2908.     {
  2909.       elf32_hppa_hash_table(link_info)->args_hash_table = NULL;
  2910.       free (args_hash_table);
  2911.     }
  2912.   return false;
  2913. }
  2914.  
  2915. /* Misc BFD support code.  */
  2916. #define bfd_elf32_bfd_reloc_type_lookup        elf_hppa_reloc_type_lookup
  2917. #define bfd_elf32_bfd_is_local_label        hppa_elf_is_local_label
  2918.  
  2919. /* Symbol extension stuff.  */
  2920. #define bfd_elf32_set_section_contents        elf32_hppa_set_section_contents
  2921. #define elf_backend_symbol_table_processing \
  2922.   elf32_hppa_backend_symbol_table_processing
  2923. #define elf_backend_begin_write_processing \
  2924.   elf32_hppa_backend_begin_write_processing
  2925. #define elf_backend_final_write_processing \
  2926.   elf32_hppa_backend_final_write_processing
  2927.  
  2928. /* Stuff for the BFD linker.  */
  2929. #define elf_backend_relocate_section        elf32_hppa_relocate_section
  2930. #define elf_backend_add_symbol_hook        elf32_hppa_add_symbol_hook
  2931. #define elf_backend_link_output_symbol_hook \
  2932.   elf32_hppa_link_output_symbol_hook
  2933. #define bfd_elf32_bfd_link_hash_table_create \
  2934.   elf32_hppa_link_hash_table_create
  2935.  
  2936. #define TARGET_BIG_SYM        bfd_elf32_hppa_vec
  2937. #define TARGET_BIG_NAME        "elf32-hppa"
  2938. #define ELF_ARCH        bfd_arch_hppa
  2939. #define ELF_MACHINE_CODE    EM_PARISC
  2940. #define ELF_MAXPAGESIZE        0x1000
  2941.  
  2942. #include "elf32-target.h"
  2943.